Java Reference
In-Depth Information
You can use the Scanner class to read input line by line using the nextLine
method, although we won't be using nextLine very much for now. The other “next”
methods are all token -based (that is, they read single elements of input rather than
entire lines).
To k e n
A single element of input (e.g., one word, one number).
By default, the Scanner uses whitespace to separate tokens.
Whitespace
Spaces, tab characters, and newline characters.
A Scanner object looks at what the user types and uses the whitespace on the
input line to break it up into individual tokens. For example, the line of input
hello there. "how are" "you?" all-one-token
would be split into six tokens:
hello
there.
"how
are"
"you?"
all-one-token
Notice that the Scanner includes punctuation characters such as periods, question
marks, and quotation marks in the tokens it generates. It also includes dashes, so
because there is no whitespace in the middle to break it up into different tokens, we
get just one token for “all-one-token.” It's possible to control how a Scanner turns
things into tokens (a process called tokenizing the input), but we won't be doing any-
thing that fancy.
It is possible to read more than one value from a Scanner , as in:
double x = console.nextDouble();
double y = console.nextDouble();
Because there are two different calls on the console object's nextDouble
method, this code will cause the computer to pause until the user has entered two
numeric values. The values can be entered on the same line or on separate lines. In
general, the computer continues to pause for user input until it has obtained whatever
values you have asked the Scanner to obtain.
 
Search WWH ::




Custom Search