Reading Input (Scanner)
Reading Input (Scanner)
(SCANNER)
Scanner
Scanner reads formatted input and converts it
into its binary form.
Although it has always been possible to read
formatted input, it required more effort than most
programmers would prefer.
Because of the addition of Scanner, it is now easy
to read all types of numeric values, strings,
and other types of data, whether it comes from a
disk file, the keyboard, or another source.
For example, you can use Scanner to read a
number from the keyboard and assign its value
to a variable.
Scanning Basics
Once you have created a Scanner, it is a simple matter to
use it to read formatted input.
In general, a Scanner reads tokens from the underlying
source that you specified when the Scanner was created.
As it relates to Scanner, a token is a portion of input that
is delineated by a set of delimiters, which is whitespace by
default.
A token is read by matching it with a particular regular
expression, which defines the format of the data.
Although Scanner allows you to define the specific type of
expression that its next input operation will match, it includes
many predefined patterns, which match the primitive types,
such as int and double, and strings.
Thus, often you wont need to specify a pattern to match.
Example