2013-04-20 45 views
3

我只是試圖做一個簡單的類,它讓我找出一個文件的長度:運營商是不確定的

public class Size { 

    long s = 0; 
    int a; 

    public static void main(String[]args){ 
     new Size(); 
    } 

    Size(){ 

     try{ 
     FileInputStream str = new FileInputStream("E:/Eclipse/Resources/smile.jpg"); 

     while(a != null){ 
      s++; 
     } 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } 

    } 


} 

我碰到一個問題

while(a != null) 

我得到的錯誤:

The operator != is undefined for the argument type(s) int, null

任何想法爲什麼它阻止了這種情況?

+0

因爲'int'不能'null'。 – chris 2013-04-20 19:22:11

回答

12

Java中的原語類型不能是null。如果您想檢查0,請執行a != 0

+0

我一直在想,但有時流有一個0,所以它會在幾個字節後終止。 – Syntic 2013-04-20 19:24:41

0

您應該爲指針分配(或)檢查NULL值。對於整數值,將其初始化爲任意數字,如標誌並檢查該條件。 NULL can be assigned or checked only with a pointer variable

+0

好吧,所以我不能檢查null,但我怎麼才能檢查流是否已結束? – Syntic 2013-04-20 19:27:27

+0

你必須改爲設置一個標誌。即。如果數據流已結束,請將a = 0,然後檢查是否存在== 0 – 2013-04-20 19:34:18

+0

Ohhh好的我只是在數據流結束時發現該標記爲-1。 – Syntic 2013-04-20 19:41:51

3

aInteger對象,該對象可以相比null

Integer value = new Integer(a); 

while (value != null) 
{ 
    // Do stuff 
}