It does not work directly because java does not allow automatic conversion to boolean. But you can do the following:
String linha = leitor.readLine();
String saida = "";
if ( (linha = leitor.readLine()) != null) {
saida += "\n";
}
As in C and C ++, the assignment is an expression whose value is that of the variable that has just been assigned. In this case the type is String
, which is not accepted within if
. With the set of parentheses, we can isolate the value of the assignment and compare it with null
to know if the operation occurred as expected.