10 12 13 4 6 0 -2 rats
Yes.
When the "rats" is encountered,
hasNextInt()
returns false and the loop (and program) ends gracefully.
(The phrase "ends gracefully" is what programmers say when
a program ends without a crash accompanied by error messages.)
Methods of Scanner | |
---|---|
HasNext Method | Reading Method |
hasNext() | next() |
hasNextDouble() | nextDouble() |
hasNextFloat() | nextFloat() |
hasNextInt() | nextInt() |
hasNextLine() | nextLine() |
hasNextLong() | nextLong() |
There are several hasNext
methods and
corresponding next
methods.
Usually your programs will use the methods for int
and double
.
The hasNext()
and next()
methods
work with tokens which are groups of characters
delimited (surrounded) by one or more spaces.
Does hasNext()
consume any of the characters from the stream?