nextInt
reads the next token in the scanner.
According to the documentation
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
And further down
The default whitespace delimiter used by a scanner is as recognized by
Character.isWhitespace()
.
The documentation for Character.isWhitespace()
says that it returns true if the given codepoint
[...][...] is '\n', U+000A LINE FEED.[...]
Which means that \n
is a separator, thus it's not part of any token, so you don't need to skip it with a dummy call to nextLine()