【问题标题】:The operator is undefined运算符未定义
【发布时间】:2013-04-14 00:06:11
【问题描述】:

我只是尝试创建一个简单的类来计算文件的长度:

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)

我得到错误:

对于参数类型 int,null,运算符 != 未定义

任何想法为什么它会阻止条件?

【问题讨论】:

  • 因为int 不能是null

标签: java operator-keyword


【解决方案1】:

Java 中的原始类型不能是null。如果要检查 0,请执行 a != 0

【讨论】:

  • 我在想这个,但有时流有一个 0,所以它会在几个字节后终止。
【解决方案2】:

a 放入Integer 对象中,可以null 进行比较:

Integer value = new Integer(a);

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

【讨论】:

    【解决方案3】:

    您应该只为指针分配(或)检查NULL 值。对于整数值,将其初始化为任何数字,如标志并检查该条件。 NULL can be assigned or checked only with a pointer variable

    【讨论】:

    • 好的,所以我无法检查 null 但我还能如何检查流是否已结束?
    • 你必须设置一个标志。 IE。如果流已经结束,输入 a=0 然后检查是否 a==0
    • 哦,好吧,我刚刚发现当流结束时它变为 -1 的标志。
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 2020-10-10
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多