I
Ian Shef
<snip>As for your code above, why not use:
BufferedReader rdr = ...;
String line = rdr.readLine();
while (line != null)
{
line = rdr.readLine();
}
That seems much clearer to me.
In this case, clearer is not necessarily better. I see these issues:
-- In the original code, the variable line had exactly the scope that it
needed. In the "clearer" code, variable line has a greatly expanded scope
that can get you into trouble eventually. One example: Suppose that you
also have a String variable called mine. With the scope of line limited to
the for (...) statement, mistyping mine as line will lead to an error
caught at compile time. With the "clearer" code, the same mistyping will
lead to an execution bug.
-- The program text
line = rdr.readLine();
now occurs twice. This violates the principle known as Don't Repeat
Yourself. There are good reasons for following this principle.