Java Reference
In-Depth Information
When a Scanner object is first constructed, the cursor points to the beginning of
the file. But as you perform various next operations, the cursor moves forward. The
ShowSum2 program from the last section processes the file through a series of calls on
nextDouble . Let's take a moment to examine in detail how that works. Again, when
the Scanner is first constructed, the input cursor will be positioned at the beginning
of the file (indicated with an up-arrow pointing at the first character):
308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n
input
cursor
After the first call on nextDouble the cursor will be positioned in the middle of
the first line, after the token “308.2”:
308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n
input
cursor
We refer to this process as consuming input .
Consuming Input
Moving the input cursor forward past some input.
The process of consuming input doesn't actually change the file, it just changes the
corresponding Scanner object so that it is positioned at a different point in the file.
The first call on nextDouble consumes the text “308.2” from the input file and
leaves the input cursor positioned at the first character after this token. Notice that
this leaves the input cursor positioned at a space. When the second call is made on
nextDouble , the Scanner first skips past this space to get to the next token, then
consumes the text “14.9” and leaves the cursor positioned at the space that follows it:
308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n
input
cursor
The third call on nextDouble skips that space and consumes the text “7.4”:
308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n
input
cursor
 
 
Search WWH ::




Custom Search