Scanner
is used to scan through a stream of character data.
Recall, from chapter 12,
that Scanner
reads a stream of character data.
The data may be the standard input stream or a text file.
A Scanner
object constructed with System.in
scans the standard input stream for groups of characters.
Groups can be characters separated by spaces
or complete lines of text.
A group of characters separated by spaces is called a token. By default, tokens are surrounded (delimited) by spaces, tabs, or the beginning and ends of lines.
As you have seen,
a Scanner
may also be connected to a disk file.
Characters from the file are put into a stream one after another.
Scanner
methods convert tokens into specific data types.
For example,
the nextInt()
method reads in a group of characters that
can be converted to an int
,
converts the characters to an int
,
and returns that int
value.
(Review: ) if nextInt()
encounters a token that cannot be converted to an int
, what happens?