public final class Scanner extends Object implements Closeable, Iterator<String>
String.split(java.lang.String).
For input from humans, the use of locale-specific regular expressions make it not only
expensive but also somewhat unpredictable.
This class supports localized numbers and various
radixes. The input is broken into tokens by the delimiter pattern, which is
\\p{javaWhitespace} by default.
Example:
Scanner s = new Scanner("1A true");
assertEquals(26, s.nextInt(16));
assertEquals(true, s.nextBoolean());
The Scanner class is not thread-safe.
| Constructor and Description |
|---|
Scanner(File src)
Creates a
Scanner with the specified File as input. |
Scanner(File src,
String charsetName)
Creates a
Scanner with the specified File as input. |
Scanner(InputStream src)
Creates a
Scanner on the specified InputStream. |
Scanner(InputStream src,
String charsetName)
Creates a
Scanner on the specified InputStream. |
Scanner(Readable src)
Creates a
Scanner with the specified Readable as input. |
Scanner(ReadableByteChannel src)
Creates a
Scanner with the specified ReadableByteChannel as
input. |
Scanner(ReadableByteChannel src,
String charsetName)
Creates a
Scanner with the specified ReadableByteChannel as
input. |
Scanner(String src)
Creates a
Scanner on the specified string. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this
Scanner and the underlying input if the input implements
Closeable. |
Pattern |
delimiter()
Returns the delimiter
Pattern in use by this Scanner. |
String |
findInLine(Pattern pattern)
Tries to find the pattern in the input.
|
String |
findInLine(String pattern)
Compiles the pattern string and tries to find a substring matching it in the input data.
|
String |
findWithinHorizon(Pattern pattern,
int horizon)
Tries to find the pattern in the input between the current position and the specified
horizon.
|
String |
findWithinHorizon(String pattern,
int horizon)
Tries to find the pattern in the input between the current position and the specified
horizon. |
boolean |
hasNext()
Returns whether this
Scanner has one or more tokens remaining to parse. |
boolean |
hasNext(Pattern pattern)
Returns whether this
Scanner has one or more tokens remaining to parse
and the next token matches the given pattern. |
boolean |
hasNext(String pattern)
Returns
true if this Scanner has one or more tokens remaining to parse
and the next token matches a pattern compiled from the given string. |
boolean |
hasNextBigDecimal()
Returns whether the next token can be translated into a valid
BigDecimal. |
boolean |
hasNextBigInteger()
Returns whether the next token can be translated into a valid
BigInteger in the default radix. |
boolean |
hasNextBigInteger(int radix)
Returns whether the next token can be translated into a valid
BigInteger in the specified radix. |
boolean |
hasNextBoolean()
Returns whether the next token can be translated into a valid
boolean value. |
boolean |
hasNextByte()
Returns whether the next token can be translated into a valid
byte value in the default radix. |
boolean |
hasNextByte(int radix)
Returns whether the next token can be translated into a valid
byte value in the specified radix. |
boolean |
hasNextDouble()
Returns whether the next token translated into a valid
double
value. |
boolean |
hasNextFloat()
Returns whether the next token can be translated into a valid
float value. |
boolean |
hasNextInt()
Returns whether the next token can be translated into a valid
int
value in the default radix. |
boolean |
hasNextInt(int radix)
Returns whether the next token can be translated into a valid
int
value in the specified radix. |
boolean |
hasNextLine()
Returns true if there is a line terminator in the input.
|
boolean |
hasNextLong()
Returns whether the next token can be translated into a valid
long value in the default radix. |
boolean |
hasNextLong(int radix)
Returns whether the next token can be translated into a valid
long value in the specified radix. |
boolean |
hasNextShort()
Returns whether the next token can be translated into a valid
short value in the default radix. |
boolean |
hasNextShort(int radix)
Returns whether the next token can be translated into a valid
short value in the specified radix. |
IOException |
ioException()
Returns the last
IOException that was raised while reading from the underlying
input, or null if none was thrown. |
Locale |
locale()
Returns the
Locale of this Scanner. |
MatchResult |
match()
Returns the result of the last matching operation.
|
String |
next()
Returns the next token.
|
String |
next(Pattern pattern)
Returns the next token if it matches the specified pattern.
|
String |
next(String pattern)
Returns the next token if it matches the specified pattern.
|
BigDecimal |
nextBigDecimal()
Returns the next token as a
BigDecimal. |
BigInteger |
nextBigInteger()
Returns the next token as a
BigInteger in the current radix. |
BigInteger |
nextBigInteger(int radix)
Returns the next token as a
BigInteger with the specified radix. |
boolean |
nextBoolean()
Returns the next token as a
boolean. |
byte |
nextByte()
Returns the next token as a
byte in the current radix. |
byte |
nextByte(int radix)
Returns the next token as a
byte with the specified radix. |
double |
nextDouble()
Returns the next token as a
double. |
float |
nextFloat()
Returns the next token as a
float. |
int |
nextInt()
Returns the next token as an
int in the current radix. |
int |
nextInt(int radix)
Returns the next token as an
int with the specified radix. |
String |
nextLine()
Returns the skipped input and advances the
Scanner to the beginning of
the next line. |
long |
nextLong()
Returns the next token as a
long in the current radix. |
long |
nextLong(int radix)
Returns the next token as a
long with the specified radix. |
short |
nextShort()
Returns the next token as a
short in the current radix. |
short |
nextShort(int radix)
Returns the next token as a
short with the specified radix. |
int |
radix()
Return the radix of this
Scanner. |
void |
remove()
Remove is not a supported operation on
Scanner. |
Scanner |
reset()
Resets this scanner's delimiter, locale, and radix.
|
Scanner |
skip(Pattern pattern)
Tries to use specified pattern to match input starting from the current position.
|
Scanner |
skip(String pattern)
Tries to use the specified string to construct a pattern and then uses
the constructed pattern to match input starting from the current position.
|
String |
toString()
Returns a string representation of this
Scanner. |
Scanner |
useDelimiter(Pattern pattern)
Sets the delimiting pattern of this
Scanner. |
Scanner |
useDelimiter(String pattern)
Sets the delimiting pattern of this
Scanner with a pattern compiled from
the supplied string value. |
Scanner |
useLocale(Locale l)
Sets the
Locale of this Scanner to a specified Locale. |
Scanner |
useRadix(int radix)
Sets the radix of this
Scanner to the specified radix. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEachRemainingpublic Scanner(File src) throws FileNotFoundException
Scanner with the specified File as input. The default charset
is applied when reading the file.src - the file to be scanned.FileNotFoundException - if the specified file does not exist.public Scanner(File src, String charsetName) throws FileNotFoundException
Scanner with the specified File as input. The specified charset
is applied when reading the file.src - the file to be scanned.charsetName - the name of the encoding type of the file.FileNotFoundException - if the specified file does not exist.IllegalArgumentException - if the specified coding does not exist.public Scanner(String src)
Scanner on the specified string.src - the string to be scanned.public Scanner(InputStream src)
Scanner on the specified InputStream. The default charset is
applied when decoding the input.src - the InputStream to be scanned.public Scanner(InputStream src, String charsetName)
Scanner on the specified InputStream. The specified charset is
applied when decoding the input.src - the InputStream to be scanned.charsetName - the encoding type of the InputStream.IllegalArgumentException - if the specified character set is not found.public Scanner(Readable src)
Scanner with the specified Readable as input.src - the Readable to be scanned.public Scanner(ReadableByteChannel src)
Scanner with the specified ReadableByteChannel as
input. The default charset is applied when decoding the input.src - the ReadableByteChannel to be scanned.public Scanner(ReadableByteChannel src, String charsetName)
Scanner with the specified ReadableByteChannel as
input. The specified charset is applied when decoding the input.src - the ReadableByteChannel to be scanned.charsetName - the encoding type of the content.IllegalArgumentException - if the specified character set is not found.public void close()
Scanner and the underlying input if the input implements
Closeable. If the Scanner has been closed, this method will have
no effect. Any scanning operation called after calling this method will throw
an IllegalStateException.close in interface Closeableclose in interface AutoCloseableCloseablepublic Pattern delimiter()
Pattern in use by this Scanner.Pattern in use by this Scanner.public String findInLine(Pattern pattern)
Scanner will advance to the end of the matched string.
Otherwise, null will be returned and the Scanner will not advance.
When waiting for input, the Scanner may be blocked. All the
input may be cached if no line terminator exists in the buffer.pattern - the pattern to find in the input.null if the pattern is not found
before the next line terminator.IllegalStateException - if the Scanner is closed.public String findInLine(String pattern)
findInLine(Pattern.compile(pattern)).pattern - a string used to construct a pattern which is in turn used to
match a substring of the input data.null if the pattern is not found
before the next line terminator.IllegalStateException - if the Scanner is closed.findInLine(Pattern)public String findWithinHorizon(Pattern pattern, int horizon)
Scanner will advance to the end of the
matched string. Otherwise, null will be returned and Scanner will not
advance. When waiting for input, the Scanner may be blocked.
The Scanner's search will never go more than horizon code points from current
position. The position of horizon does have an effect on the result of the
match. For example, when the input is "123" and current position is at zero,
findWithinHorizon(Pattern.compile("\\p{Digit}{3}"), 2)
will return null, while
findWithinHorizon(Pattern.compile("\\p{Digit}{3}"), 3)
will return "123". horizon is treated as a transparent,
non-anchoring bound. (refer to
Matcher.useTransparentBounds(boolean) and
Matcher.useAnchoringBounds(boolean))
A horizon whose value is zero will be ignored and the whole input will be
used for search. In this situation, all the input may be cached.
pattern - the pattern used to scan.horizon - the search limit.null if the pattern is not found
within the specified horizon.IllegalStateException - if the Scanner is closed.IllegalArgumentException - if horizon is less than zero.public String findWithinHorizon(String pattern, int horizon)
horizon. Delimiters are ignored. This call is the same as invoking
findWithinHorizon(Pattern.compile(pattern)).pattern - the pattern used to scan.horizon - the search limit.null if the pattern is not found
within the specified horizon.IllegalStateException - if the Scanner is closed.IllegalArgumentException - if horizon is less than zero.findWithinHorizon(Pattern, int)public boolean hasNext()
Scanner has one or more tokens remaining to parse.
This method will block if the data is still being read.hasNext in interface Iterator<String>true if this Scanner has one or more tokens remaining,
otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNext(Pattern pattern)
Scanner has one or more tokens remaining to parse
and the next token matches the given pattern. This method will block if the data is
still being read.pattern - the pattern to check for.true if this Scanner has more tokens and the next token
matches the pattern, false otherwise.IllegalStateException - if the Scanner has been closed.public boolean hasNext(String pattern)
true if this Scanner has one or more tokens remaining to parse
and the next token matches a pattern compiled from the given string. This method will
block if the data is still being read. This call is equivalent to
hasNext(Pattern.compile(pattern)).pattern - the string specifying the pattern to scan fortrue if the specified pattern matches this Scanner's
next token, false otherwise.IllegalStateException - if the Scanner has been closed.public boolean hasNextBigDecimal()
BigDecimal.true if the next token can be translated into a valid
BigDecimal, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextBigInteger()
BigInteger in the default radix.true if the next token can be translated into a valid
BigInteger, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextBigInteger(int radix)
BigInteger in the specified radix.radix - the radix used to translate the token into a
BigInteger.true if the next token can be translated into a valid
BigInteger, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextBoolean()
boolean value.true if the next token can be translated into a valid
boolean value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextByte()
byte value in the default radix.true if the next token can be translated into a valid
byte value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextByte(int radix)
byte value in the specified radix.radix - the radix used to translate the token into a byte
valuetrue if the next token can be translated into a valid
byte value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextDouble()
double
value.true if the next token can be translated into a valid
double value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextFloat()
float value.true if the next token can be translated into a valid
float value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextInt()
int
value in the default radix.true if the next token can be translated into a valid
int value, otherwise false.IllegalStateException - if the Scanner has been closed,public boolean hasNextInt(int radix)
int
value in the specified radix.radix - the radix used to translate the token into an int
value.true if the next token in this Scanner's input can be
translated into a valid int value, otherwise
false.IllegalStateException - if the Scanner has been closed.public boolean hasNextLine()
IllegalStateException - if this Scanner is closed.public boolean hasNextLong()
long value in the default radix.true if the next token can be translated into a valid
long value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextLong(int radix)
long value in the specified radix.radix - the radix used to translate the token into a long
value.true if the next token can be translated into a valid
long value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextShort()
short value in the default radix.true if the next token can be translated into a valid
short value, otherwise false.IllegalStateException - if the Scanner has been closed.public boolean hasNextShort(int radix)
short value in the specified radix.radix - the radix used to translate the token into a short
value.true if the next token can be translated into a valid
short value, otherwise false.IllegalStateException - if the Scanner has been closed.public IOException ioException()
IOException that was raised while reading from the underlying
input, or null if none was thrown.public Locale locale()
Locale of this Scanner.public MatchResult match()
The next* and find* methods return the match result in the case of a successful match.
IllegalStateException - if the match result is not available, of if the last match
was not successful.public String next()
next in interface Iterator<String>IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.public String next(Pattern pattern)
pattern - the specified pattern to scan.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token does not match the pattern given.public String next(String pattern)
next(Pattern.compile(pattern)).pattern - the string specifying the pattern to scan for.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token does not match the pattern given.public BigDecimal nextBigDecimal()
BigDecimal. This method will block if input is
being read. If the next token can be translated into a BigDecimal
the following is done: All Locale-specific prefixes, group separators,
and Locale-specific suffixes are removed. Then non-ASCII digits are
mapped into ASCII digits via Character.digit(char, int), and a
negative sign (-) is added if the Locale-specific negative prefix or
suffix was present. Finally the resulting string is passed to
BigDecimal(String) .BigDecimal.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
BigDecimal.public BigInteger nextBigInteger()
BigInteger in the current radix.
This method may block for more input.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
BigInteger.public BigInteger nextBigInteger(int radix)
BigInteger with the specified radix.
This method will block if input is being read. If the next token can be translated
into a BigInteger the following is done: All Locale-specific
prefixes, group separators, and Locale-specific suffixes are removed.
Then non-ASCII digits are mapped into ASCII digits via
Character.digit(char, int), and a negative sign (-) is added if the
Locale-specific negative prefix or suffix was present. Finally the
resulting String is passed to BigInteger(String, int)}
with the specified radix.radix - the radix used to translate the token into a
BigInteger.BigIntegerIllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
BigInteger.public boolean nextBoolean()
boolean. This method will block if input is
being read.boolean.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
boolean value.public byte nextByte()
byte in the current radix.
This method may block for more input.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
byte value.public byte nextByte(int radix)
byte with the specified radix. Will
block if input is being read. If the next token can be translated into a
byte the following is done: All Locale-specific prefixes, group
separators, and Locale-specific suffixes are removed. Then non-ASCII
digits are mapped into ASCII digits via
Character.digit(char, int), and a negative sign (-) is added if the
Locale-specific negative prefix or suffix was present. Finally the
resulting String is passed to Byte.parseByte(String, int)} with
the specified radix.radix - the radix used to translate the token into byte value.byte.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
byte value.public double nextDouble()
double. This method will block if input is being
read. If the next token can be translated into a double the
following is done: All Locale-specific prefixes, group separators, and
Locale-specific suffixes are removed. Then non-ASCII digits are mapped
into ASCII digits via Character.digit(char, int), and a negative
sign (-) is added if the Locale-specific negative prefix or suffix was
present. Finally the resulting String is passed to
Double.parseDouble(String)}. If the token matches the localized
NaN or infinity strings, it is also passed to
Double.parseDouble(String)}.double.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
double value.public float nextFloat()
float. This method will block if input is being
read. If the next token can be translated into a float the
following is done: All Locale-specific prefixes, group separators, and
Locale-specific suffixes are removed. Then non-ASCII digits are mapped
into ASCII digits via Character.digit(char, int), and a negative
sign (-) is added if the Locale-specific negative prefix or suffix was
present. Finally the resulting String is passed to
Float.parseFloat(String)}.If the token matches the localized NaN
or infinity strings, it is also passed to
Float.parseFloat(String)}.float.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
float value.public int nextInt()
int in the current radix.
This method may block for more input.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
int value.public int nextInt(int radix)
int with the specified radix. This method will
block if input is being read. If the next token can be translated into an
int the following is done: All Locale-specific prefixes, group
separators, and Locale-specific suffixes are removed. Then non-ASCII
digits are mapped into ASCII digits via
Character.digit(char, int), and a negative sign (-) is added if the
Locale-specific negative prefix or suffix was present. Finally the
resulting String is passed to Integer.parseInt(String, int) with
the specified radix.radix - the radix used to translate the token into an int
value.int.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
int value.public String nextLine()
Scanner to the beginning of
the next line. The returned result will exclude any line terminator. When
searching, if no line terminator is found, then a large amount of input
will be cached. If no line at all can be found, a NoSuchElementException
will be thrown.IllegalStateException - if the Scanner is closed.NoSuchElementException - if no line can be found, e.g. when input is an empty string.public long nextLong()
long in the current radix.
This method may block for more input.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
long value.public long nextLong(int radix)
long with the specified radix. This method will
block if input is being read. If the next token can be translated into a
long the following is done: All Locale-specific prefixes, group
separators, and Locale-specific suffixes are removed. Then non-ASCII
digits are mapped into ASCII digits via
Character.digit(char, int), and a negative sign (-) is added if the
Locale-specific negative prefix or suffix was present. Finally the
resulting String is passed to Long.parseLong(String, int)} with
the specified radix.radix - the radix used to translate the token into a long
value.long.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
long value.public short nextShort()
short in the current radix.
This method may block for more input.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
short value.public short nextShort(int radix)
short with the specified radix. This method will
block if input is being read. If the next token can be translated into a
short the following is done: All Locale-specific prefixes, group
separators, and Locale-specific suffixes are removed. Then non-ASCII
digits are mapped into ASCII digits via
Character.digit(char, int), and a negative sign (-) is added if the
Locale-specific negative prefix or suffix was present. Finally the
resulting String is passed to Short.parseShort(String, int)}
with the specified radix.radix - the radix used to translate the token into short
value.short.IllegalStateException - if this Scanner has been closed.NoSuchElementException - if input has been exhausted.InputMismatchException - if the next token can not be translated into a valid
short value.public int radix()
Scanner.Scannerpublic Scanner skip(Pattern pattern)
NoSuchElementException will be thrown.
Patterns that can match a lot of input may cause the Scanner to read
in a large amount of input.pattern - used to skip over input.Scanner itself.IllegalStateException - if the Scanner is closed.NoSuchElementException - if the specified pattern match fails.public Scanner skip(String pattern)
skip(Pattern.compile(pattern)).pattern - the string used to construct a pattern which in turn is used to
match input.Scanner itself.IllegalStateException - if the Scanner is closed.public String toString()
Scanner. The information
returned may be helpful for debugging. The format of the string is unspecified.public Scanner useDelimiter(Pattern pattern)
Scanner.pattern - the delimiting pattern to use.Scanner.public Scanner useDelimiter(String pattern)
Scanner with a pattern compiled from
the supplied string value.pattern - a string from which a Pattern can be compiled.Scanner.public Scanner useLocale(Locale l)
Locale of this Scanner to a specified Locale.l - the specified Locale to use.Scanner.public Scanner useRadix(int radix)
Scanner to the specified radix.radix - the specified radix to use.Scanner.public void remove()
Scanner.remove in interface Iterator<String>UnsupportedOperationException - if this method is invoked.public Scanner reset()