Java Reference
In-Depth Information
Notice one other thing of interest in this program. There are two do - while loops in the
program. The first loops until the user guesses the letter. Its operation and meaning should
be clear. The second do - while loop, shown again here, warrants some explanation:
As explained earlier, console input is line buffered—you have to press ENTER before charac-
ters are sent. Pressing ENTER causes a carriage return and a line feed (newline) sequence to
be generated. These characters are left pending in the input buffer. Also, if you typed more
than one key before pressing ENTER , they too would still be in the input buffer. This loop
discards those characters by continuing to read input until the end of the line is reached. If
they were not discarded, then those characters would also be sent to the program as guesses,
which is not what is wanted. (To see the effect of this, you might try removing the inner
do - while loop.) In Chapter 10 , after you have learned more about Java, some other, higher-
level ways of handling console input are described. However, the use of read( ) here gives
you insight into how the foundation of Java's I/O system operates. It also shows another
example of Java's loops in action.
Try This 3-2 Improve the Java Help System
Search WWH ::




Custom Search