Java - while循环

时间:2015-11-20 19:53:11

标签: java while-loop null boolean objectinputstream

非常简单的问题,但是我想使用while循环等待输入到我的ObjectInputStream,如下所示:

while ((messageObject = objectInputStream.readObject()) != null) {
    // Do something once object is passed through inputstream
}

我如何使用ObjectInputStream的readBoolean方法实现它,而不将boolean转换为其他东西?

例如:

while ((messageBoolean = objectInputStream.readBoolean()) != null) {
    // Do something once boolean is passed through inputstream
}

上面的代码只是给出了编译时错误:无法比较的类型:boolean和<'null>

编辑:我应该澄清一下:

objectInputStream = ObjectInputStream(socket.getInputStream())

1 个答案:

答案 0 :(得分:1)

您可以这样做:

boolean valueRead=false;
boolean endOfFile=false;
while(!endOfFile) {
    try { 
        valueRead = objectInputStream.readBoolean();
        //Do something with the boolean 
    } catch (EOFException e) {
        endOfFile = true;
    } 
}

查看readBoolean