page 624might 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 indicate 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 and added to the sum. Otherwise,
the value is read (as a String) with next and ignored. The hasNext and next methods should be
familiar to youthey are two of the methods of Iterator. And indeed Scanner implements
Iterator
method of Scanner tHRows UnSupportedOperationException.
[3] Note that Scanner is an Iterator not an Iterable, so it can not be used with the
enhanced for loop. This limitation may be removed in the future.
The hasNext method returns TRue if the scanner has another token to return from next. For each primitive
type except char, hasNextType asks if the next token is of that type, and if so, nextType will return it.
For the integer types there are overloads of hasNextType and nextType that take an int radix for the
number. For example, hasNextInt(8) will return TRue if the next token is a valid octal representation of
an int value. The default radix of the scanner (initially 10) can be changed with the useRadix method, and
retrieved from the radix method.
The hasNext method also has two additional overloads that take a pattern and will return true only if the
next token matches the given pattern. Similarly, next also has two overloads that take a pattern describing
the token to return.
All the methods that get the next token operate as follows: Delimiters from the current position to the first
non-delimiter are ignored; then the next delimiter is found; if the input between the delimiters matches the
pattern then a token has been found and the current position is advanced to the first character after the token.
If there is no next token then NoSuchElementException is thrown. If the method requires a specific
type of token or a token that matches a given radix or pattern, and the next token does not meet those criteria,
then InputMismatchException is thrown. If an exception is thrown then the position of the scanner is
unchanged.
Input sources for scanners can come from several places. There is a constructor that takes a Readable
character source, one that takes a String, and several that take byte sources: File, InputStream or
java.nio.ReadableByteBuffer. By default these byte sources will be converted to characters using
the platform's default encoding, or you can use a form of the constructor that takes the character set name to
use as the encoding.
When the source for a scanner is a file or other input stream, it is possible that when the scanner tries to read
the source it will encounter an IOException. The scanner methods themselves do not declare or throw the
IOException. The responsibility for dealing with these potential exceptions falls to you. If the scanner
encounters an IOException it considers the end of the input source to have been reachedso hasNext will
return false, and any attempt to take the next token will cause NoSuchElementException to be thrown.
To determine whether tokenizing of the source ended because of a true end of input or an exception, you can