It is neither a design flaw nor a bug. It is a poorly‑documented feature.
Knute Snortum wrote:. . . . Scanner has a design flaw *cough*bug*cough* where it will leave an "enter" key press (CRLF on Windows) in its buffer. . . .
123 456 789
Now, we all know the ints you are trying to read are 123 456 and 789, not “123 ” etc. If you leave the following space behind because it is part of the delimiter, you are also going to leave any following line end characters behind, because they are also part of the delimiter. It says quite clearly in the method documentation that nextLine() returns
123 456 789
That is crystal‑clear, but many people who write Java® books never seem to read it.
the rest of the current line, excluding any line separator at the end.
Note there is a space after the number.
Earlier in this post, I wrote:not “123 ” etc.
And before the release of Scanner, so either buffered reader or JOptionPane were the standard means for getting keyboard input.
Matthew Bendford wrote:. . . The book was about 1.3 - so even before the release of JavaSE1.4.
I have a Java1.3 book, too; when I used it I was taught that many people wrote their own keyboard input classes; I did, using buffered readers, and it is typical beginner's code. When Scanner came out we were told that it was no longer necessary to write one's own utility, but I have since realised that such a utility is useful. And it uses the power of Scanner; it will return an int without throwing exceptions if the wrong input is given.
If Scanner . . . were taught so that beginners can fully grasp its pitfalls . . .