Roman
Roman

Reputation: 2355

BufferedReader readLine() method returns boolean?

I am writing a chat client in Java and getting a weird error message while trying to compile this in Eclipse 4.4.1:

            ...
            BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            ...
            String answer;
            while (answer = socketIn.readLine() != null) {
                incomingTextField.setText(answer);
            }
            ...

The error is:

Type mismatch: cannot convert from boolean to String

There is also appearing tip:

Change type of 'answer' to 'boolean'

However, this makes no sense, as according to the documentation readLine() must return String.

Upvotes: 0

Views: 2424

Answers (1)

Jan
Jan

Reputation: 13858

You need some () here:

while ((answer = socketIn.readLine()) != null) {

Check about Operator Precedence to figure out why

Good Luck

Upvotes: 11

Related Questions