Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 1.04 KB

67dd.md

File metadata and controls

37 lines (28 loc) · 1.04 KB

Back to questions

Solution to 67dd: Word count

See code at solutions/code/tutorialquestions/question67dd

The sample source code solution should be fairly straightforward: check that you understand it. The solution illustrates quite an ugly construct in Java: the expression

(line = br.readLine()) != null

which has the effect of calling readLine() on br, returning the result into line, then comparing line with null. This use of expressions that produce side-effects is quite poor programming style in general: it can make code difficult to understand. However, it is frequently used when processing input, as follows (where br is a BufferedReader):

String line;
while((line = br.readLine()) != null) {
   ...
}

You can see that this is a bit more concise than the equivalent form that does not use a side-effecting expression:

String line = br.readLine();
while(line != null) {
   ...
   line = br.readLine();
}

You will see the more concise form a lot in practice.