This
class is
a text scanner or tokenizer. It can read input from any
Readable object, and convenience constructors can
read text from a specified string, file, byte stream, or byte
channel. The constructors for files, byte streams, and byte channels
optionally allow you to specify the name of the charset to use for
byte-to-character conversions.
After creating a Scanner, you can configure it.
useDelimiter( ) specifies a regular expression (as
a java.util.regex.Pattern or a
String) that represents the token delimiter. The
default delimiter is any run of whitespace. useLocale(
) specifies the Locale to use for
scanning numbers: this may affect things like the character expected
for decimal points and the thousands separator. useRadix(
) specifies the radix, or base, in which numbers should be
parsed. Any value between 2 and 36 is allowed. These configuration
methods may be called at any time and are not required to be called
before scanning begins.
Scanner implements the
Iterable<String> interface, and you can use
the hasNext( ) and next( )
methods of this interface to break the input into a series of
String tokens separated by whitespace or by the
delimiter specified with useDelimiter( ). In
addition to these Iterable methods, however,
Scanner defines a number of
nextX and
hasNextX methods for
various numeric types X.
nextLine( ) returns the next line of input. Two
variants of the next( ) method accept a regular
expression as an argument and return the next chunk of text matching
a specified regular expression. The corresponding hasNext(
) methods accept a regular expression and return
TRue if the input matches it.
The skip( ) method ignores delimiters and skips
text matching the specified regular expression. findInLine(
) looks ahead for text matching the specified regular
expression in the current line. If a match is found, the
Scanner advances past that text and returns it.
Otherwise, the Scanner returns
null without advancing.
findWithinHorizon( ) is similar but looks for a
match within the specified number of characters (a horizon of 0
specifies an unlimited number).
The next( ) methods and its
nextX variants throw a
NoSuchElementException if there is no more input
text. They throw an InputMismatchException (a
subclass of NoSuchElementException) if the next
token cannot be parsed as the specified type or does not match the
specified pattern. The Readable object that the
Scanner reads text from may throw a
java.io.IOException, but, for ease of use, the
Scanner never propagates this exception. If an
IOException occurs, the Scanner
assumes that no more input is available from the
Readable. Call ioException( )
to obtain the most recent IOException, if any,
thrown by the Readable.
The close( ) method checks whether the
Readable object implements the
Closeable interface and, if so, calls the
close( ) method on that object. Once
close( ) has been called, any attempt to read
tokens from the Scanner results in an
IllegalStateException.
See also StringTokenizer and
java.io.StreamTokenizer.
Figure 16-53. java.util.Scanner
public final class
Scanner implements Iterator<String> {
// Public Constructors
public
Scanner (Readable
source );
public
Scanner (java.nio.channels.ReadableByteChannel
source );
public
Scanner (java.io.InputStream
source );
public
Scanner (java.io.File
source ) throws java.io.FileNotFoundException;
public
Scanner (String
source );
public
Scanner (java.nio.channels.ReadableByteChannel
source , String
charsetName );
public
Scanner (java.io.InputStream
source , String
charsetName );
public
Scanner (java.io.File
source , String
charsetName )
throws java.io.FileNotFoundException;
// Public Instance Methods
public void
close ( );
public java.util.regex.Pattern
delimiter ( );
public String
findInLine (String
pattern );
public String
findInLine (java.util.regex.Pattern
pattern );
public String
findWithinHorizon (java.util.regex.Pattern
pattern , int
horizon );
public String
findWithinHorizon (String
pattern , int
horizon );
public boolean
hasNext (java.util.regex.Pattern
pattern );
public boolean
hasNext (String
pattern );
public boolean
hasNextBigDecimal ( );
public boolean
hasNextBigInteger ( );
public boolean
hasNextBigInteger (int
radix );
public boolean
hasNextBoolean ( );
public boolean
hasNextByte ( );
public boolean
hasNextByte (int
radix );
public boolean
hasNextDouble ( );
public boolean
hasNextFloat ( );
public boolean
hasNextInt ( );
public boolean
hasNextInt (int
radix );
public boolean
hasNextLine ( );
public boolean
hasNextLong ( );
public boolean
hasNextLong (int
radix );
public boolean
hasNextShort ( );
public boolean
hasNextShort (int
radix );
public java.io.IOException
ioException ( );
public Locale
locale ( );
public java.util.regex.MatchResult
match ( );
public String
next (String
pattern );
public String
next (java.util.regex.Pattern
pattern );
public java.math.BigDecimal
nextBigDecimal ( );
public java.math.BigInteger
nextBigInteger ( );
public java.math.BigInteger
nextBigInteger (int
radix );
public boolean
nextBoolean ( );
public byte
nextByte ( );
public byte
nextByte (int
radix );
public double
nextDouble ( );
public float
nextFloat ( );
public int
nextInt ( );
public int
nextInt (int
radix );
public String
nextLine ( );
public long
nextLong ( );
public long
nextLong (int
radix );
public short
nextShort ( );
public short
nextShort (int
radix );
public int
radix ( );
public Scanner
skip (java.util.regex.Pattern
pattern );
public Scanner
skip (String
pattern );
public Scanner
useDelimiter (java.util.regex.Pattern
pattern );
public Scanner
useDelimiter (String
pattern );
public Scanner
useLocale (Locale
locale );
public Scanner
useRadix (int
radix );
// Methods Implementing Iterator
public boolean
hasNext ( );
public String
next ( );
public void
remove ( );
// Public Methods Overriding Object
public String
toString ( );
}