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.)
| 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.
A token i a group of characters
delimited (surrounded) by one or more spaces.
The token can be any group of contiguous characters, not necessarily characters that can be converted into a numeric data type.
Does hasNext() consume any of the characters from the stream?