Java Reference
In-Depth Information
result += in.nextDouble();
else
in.next();
}
IOException ex = in.ioException();
if (ex != null)
throw ex;
return result;
}
You can iterate through the input one token at a time, asking if the next
token is a string, int , or double value, and so forth, and storing it in-
to a variable of the appropriate type. [2] In contrast to StreamTokenizer ,
Scanner can produce primitive values other than just double , and it re-
cognizes numbers in a range of formats, not just the common decimal
format. (Essentially, how ever Formatter see page 624 might write out a
number as a string, Scanner can read it back as a number.)
[2]
The Scanner class
also
supports
scanning
of
values
into java.math.BigInteger and
java.math.BigDecimal objects.
A scanner needs a source of characters to read from, which in the
most general case is represented by an object that implements the
java.lang.Readable interfacethis interface defines a single method, read ,
that takes a java.nio.CharBuffer object into which the characters read
from the Readable should be stored. The sumStream method accepts a
Readable parameter and passes it to the constructor for Scanner to in-
dicate it should be used as the input source for that scanner. The
java.io.Reader class implements Readable , so any of the input character
streams can be used as a source for a Scanner object.
The loop continues as long as hasNext indicates that the scanner has
another token available. Knowing that there is a token doesn't tell you
what kind of token it may be, so we use the hasNextDouble method to
ask if the token is a double value. If so, the value is read with nextdouble
 
 
Search WWH ::




Custom Search