is new.
java.lang.Objectjava.util.Scanner
public final class Scanner
A simple text scanner which can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
For example, this code allows a user to read a number from System.in :
Scanner sc =new Scanner(System.in);
Scanner.create(System.in);int i = sc.nextInt();
As another example, this code allows long types to be assigned from entries in a file myNumbers:
Scanner sc =new Scanner(new
Scanner.create(newFile("myNumbers")); while (sc.hasNextLong()) { long aLong = sc.nextLong(); }
The scanner can also use delimiters other than whitespace. This example reads several items in from a string:
String input = "1 fish 2 fish red fish blue fish"; Scanner s =new Scanner(input).useDelimiter("\\s*fish\\s*");
Scanner.create(input).useDelimiter("\\s*fish\\s*");System.out.println(s.nextInt()); System.out.println(s.nextInt()); System.out.println(s.next()); System.out.println(s.next()); s.close();
prints the following output:
1
2
red
blue
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:
String input = "1 fish 2 fish red fish blue fish"; Scanner s =new Scanner(input);
Scanner.create(input);s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"); MatchResult result = s.match(); for (int i=1; i <=result.groupCount(); i++) System.out.println(result.group(i); s.close();
The default whitespace delimiter used by a scanner is as recognized by Character . isWhitespace .
A scanning operation may block waiting for input.
The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt() ) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.
The findInLine(java.lang.String) , findWithinHorizon(java.lang.String, int) , and skip(java.util.regex.Pattern) methods operate independently of the delimiter pattern. These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant. These methods may block waiting for more input.
When a scanner throws an InputMismatchException , the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern
"\\s+"
"\\w+"
will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern
"\\s"
"\\w"
could return empty tokens since it only passes one space at a time.
A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable's Readable.read(java.nio.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.
When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
A Scanner is not safe for multithreaded use without external synchronization.
Unless otherwise mentioned, passing a null parameter into any method of a Scanner will cause a NullPointerException to be thrown.
A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the useRadix method.
An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the
Locale.getDefault()
method; it may be changed via the
useLocale(java.util.Locale)
method.
The localized formats are defined in terms of the following parameters, which for a particular locale are taken from that locale's
DecimalFormat
object,
df
, and its and
DecimalFormatSymbols
object,
dfs
.
The strings that can be parsed as numbers by an instance of this class are specified in terms of the following regular-expression grammar, where Rmax is the highest digit in the radix being used (for example, Rmax is 9 in base 10).
The strings that can be parsed as numbers by an instance of this class are specified in terms of the following regular-expression grammar, where Rmax is the highest digit in the radix being used (for example, Rmax is 9 in base 10).
Whitespace is not significant in the above regular expressions.
A scanner's locale affects many elements of its default primitive matching regular expressions; see
localized numbers
above.
A scanner's
A scanner's radix affects elements of its default number matching regular expressions; see
localized numbers
above.
An invocation of this method of the form
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
An invocation of this method of the form
findInLine(pattern)
behaves in exactly the same way as the invocation
findInLine(Pattern.compile(pattern))
.
Since this method continues to search through the input looking for the specified pattern, it may buffer all of the input searching for the desired token if no line separators are present.
An invocation of this method of the form
findWithinHorizon(pattern)
behaves in exactly the same way as the invocation
findWithinHorizon(Pattern.compile(pattern, horizon))
.
This method searches through the input up to the specified search horizon, ignoring delimiters. If the pattern is found the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected then the null is returned and the scanner's position remains unchanged. This method may block waiting for input that matches the pattern.
A scanner will never search more than horizon code points beyond its current position. Note that a match may be clipped by the horizon; that is, an arbitrary match result may have been different if the horizon had been larger. The horizon is treated as the end of input by the matching engine, therefore constructs that rely upon the end of input such as '$' may match against it.
If horizon is 0, then the horizon is ignored and this method continues to search through the input looking for the specified pattern without bound. In this case it may buffer all of the input searching for the pattern.
If horizon is negative, then an IllegalArgumentException is thrown.
If a match to the specified pattern is not found at the current position, then no input is skipped and a
NoSuchElementException
is thrown.
Since this method seeks to match the specified pattern starting at the scanner's current position, patterns that can match a lot of input (".*", for example) may cause the scanner to buffer a large amount of input.
Note that it is possible to skip something without risking a NoSuchElementException by using a pattern that can match nothing, e.g., sc.skip("[ \t]*").
An invocation of this method of the form
skip(pattern)
behaves in exactly the same way as the invocation
skip(Pattern.compile(pattern))
.
An invocation of this method of the form
nextByte()
behaves in exactly the same way as the invocation
nextByte(radix)
, where radix
If the next token matches the
Integer
regular expression
An invocation of this method of the form
nextShort()
behaves in exactly the same way as the invocation
nextShort(radix)
, where radix
If the next token matches the
Integer
regular expression defined above then the token is converted into a
short
value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via
Character.digit
, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to
Short.parseShort
with the specified radix.
An invocation of this method of the form
nextInt()
behaves in exactly the same way as the invocation
nextInt(radix)
, where radix
If the next token matches the
Integer
regular expression defined above then the token is converted into an
int
value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via
Character.digit
, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to
Integer.parseInt
with the specified radix.
An invocation of this method of the form
nextLong()
behaves in exactly the same way as the invocation
nextLong(radix)
, where radix
If the next token matches the
Integer
regular expression defined above then the token is converted into an
long
value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via
Character.digit
, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to
Long.parseLong
with the specified radix.
If the next token matches the
Float
regular expression defined above then the token is converted into a
float
value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via
Character.digit
, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to
Float.parseFloat
. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed to
Float.parseFloat
as appropriate.
If the next token matches the
Float
regular expression defined above then the token is converted into a
double
value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via
Character.digit
, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to
Double.parseDouble
. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed to
Double.parseDouble
as appropriate.
An invocation of this method of the form
nextBigInteger()
behaves in exactly the same way as the invocation
nextBigInteger(radix)
, where radix
If the next token matches the
Integer
regular expression defined above then the token is converted into a
BigInteger
value as if by removing all group separators, mapping non-ASCII digits into ASCII digits via the
Character.digit
, and passing the resulting string to the
BigInteger(String, int)
constructor with the specified radix.
If the next token matches the
Decimal
regular expression defined above then the token is converted into a
BigDecimal
value as if by removing all group separators, mapping non-ASCII digits into ASCII digits via the
Character.digit
, and passing the resulting string to the
BigDecimal(String)
constructor.
Localized numbers
LocalGroupSeparator
The character used to separate thousands groups,
i.e.,
dfs.
getGroupingSeparator()
LocalDecimalSeparator
The character used for the decimal point,
i.e.,
dfs.
getDecimalSeparator()
LocalPositivePrefix
The string that appears before a positive number (may be empty),
i.e.,
df.
getPositivePrefix()
LocalPositiveSuffix
The string that appears after a positive number (may be empty),
i.e.,
df.
getPositiveSuffix()
LocalNegativePrefix
The string that appears before a negative number (may be empty),
i.e.,
df.
getNegativePrefix()
LocalNegativeSuffix
The string that appears after a negative number (may be empty),
i.e.,
df.
getNegativeSuffix()
LocalNaN
The string that represents not-a-number for floating-point values,
i.e.,
dfs.
getInfinity()
LocalInfinity
The string that represents infinity for floating-point values,
i.e.,
dfs.
getInfinity()
Number syntax
NonASCIIDigit
::
= A non-ASCII character c for which
Character.isDigit
Number syntax
NonASCIIDigit
::
= A non-ASCII character c for which
Character.isDigit
(c)
returns true
Non0Digit
::
= [1-
Rmax
] |
NonASCIIDigit
Digit
::
= [0-
Rmax
] |
NonASCIIDigit
GroupedNumeral
::
= (
Non0Digit
Digit
?
Digit
?
(
LocalGroupSeparator
Digit
Digit
Digit
)+ )
Numeral
::
= ( (
Digit
+ ) |
GroupedNumeral
)
Integer
::
= ( [-+]? (
Numeral
) )
|
LocalPositivePrefix
Numeral
LocalPositiveSuffix
|
LocalNegativePrefix
Numeral
LocalNegativeSuffix
DecimalNumeral
::
=
Numeral
|
Numeral
LocalDecimalSeparator
Digit
*
|
LocalDecimalSeparator
Digit
+
Exponent
::
= ( [eE] [+-]?
Digit
+ )
Decimal
::
= ( [-+]?
DecimalNumeral
Exponent
? )
|
LocalPositivePrefix
DecimalNumeral
LocalPositiveSuffix
Exponent
?
|
LocalNegativePrefix
DecimalNumeral
LocalNegativeSuffix
Exponent
?
HexFloat
::
= [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)?
NonNumber
::
= NaN |
LocalNan
| Infinity |
LocalInfinity
SignedNonNumber
::
= ( [-+]?
NonNumber
)
|
LocalPositivePrefix
NonNumber
LocalPositiveSuffix
|
LocalNegativePrefix
NonNumber
LocalNegativeSuffix
Float
::
=
Decimal
|
HexFloat
|
SignedNonNumber
Constructor Summary
Scanner
(
File
Constructs a new Scanner that produces values scanned from the specified file.
Scanner
(
File
source,
String
Constructs a new Scanner that produces values scanned from the specified file.
Scanner
(
InputStream
Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner
(
InputStream
source,
String
Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner
(
Readable
Constructs a new Scanner that produces values scanned from the specified source.
Scanner
(
ReadableByteChannel
Constructs a new Scanner that produces values scanned from the specified channel.
Scanner
(
ReadableByteChannel
source,
String
Constructs a new Scanner that produces values scanned from the specified channel.
Scanner
(
String
Constructs a new Scanner that produces values scanned from the specified string.
Method Summary
void
close
()
Closes this scanner.
static
Scanner
create
(
File
Returns a Scanner that produces values scanned from the specified file.
static
Scanner
create
(
File
source,
String
Returns a Scanner that produces values scanned from the specified file.
static
Scanner
create
(
InputStream
Returns a Scanner that produces values scanned from the specified input stream.
static
Scanner
create
(
InputStream
source,
String
Returns a Scanner that produces values scanned from the specified input stream.
static
Scanner
create
(
Readable
Returns a Scanner that produces values scanned from the specified source.
static
Scanner
create
(
ReadableByteChannel
Returns a Scanner that produces values scanned from the specified channel.
static
Scanner
create
(
ReadableByteChannel
source,
String
Returns a Scanner that produces values scanned from the specified channel.
static
Scanner
create
(
String
Returns a Scanner that produces values scanned from the specified string.
Pattern
delimiter
()
Returns the Pattern this Scanner is currently using to match delimiters.
String
findInLine
(
Pattern
pattern)
Attempts to find the next occurrence of the specified pattern ignoring delimiters.
String
findInLine
(
String
pattern)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
String
findWithinHorizon
(
Pattern
pattern, int horizon)
Attempts to find the next occurrence of the specified pattern.
String
findWithinHorizon
(
String
pattern, int horizon)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
boolean
hasNext
()
Returns true if this scanner has another token in its input.
boolean
hasNext
(
Pattern
pattern)
Returns true if the next complete token matches the specified pattern.
boolean
hasNext
(
String
pattern)
Returns true if the next token matches the pattern constructed from the specified string.
boolean
hasNextBigDecimal
()
Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the
nextBigDecimal()
method.
boolean
hasNextBigInteger
()
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the
nextBigInteger()
method.
boolean
hasNextBigInteger
(int radix)
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the specified radix using the
nextBigInteger()
method.
boolean
hasNextBoolean
()
Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".
boolean
hasNextByte
()
Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the
nextByte()
method.
boolean
hasNextByte
(int radix)
Returns true if the next token in this scanner's input can be interpreted as a byte value in the specified radix using the
nextByte()
method.
boolean
hasNextDouble
()
Returns true if the next token in this scanner's input can be interpreted as a double value using the
nextDouble()
method.
boolean
hasNextFloat
()
Returns true if the next token in this scanner's input can be interpreted as a float value using the
nextFloat()
method.
boolean
hasNextInt
()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the
nextInt()
method.
boolean
hasNextInt
(int radix)
Returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the
nextInt()
method.
boolean
hasNextLine
()
Returns true if there is another line in the input of this scanner.
boolean
hasNextLong
()
Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the
nextLong()
method.
boolean
hasNextLong
(int radix)
Returns true if the next token in this scanner's input can be interpreted as a long value in the specified radix using the
nextLong()
method.
boolean
hasNextShort
()
Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the
nextShort()
method.
boolean
hasNextShort
(int radix)
Returns true if the next token in this scanner's input can be interpreted as a short value in the specified radix using the
nextShort()
method.
IOException
ioException
()
Returns the IOException last thrown by this Scanner's underlying Readable.
Locale
locale
()
Returns this scanner's locale.
MatchResult
match
()
Returns the match result of the last scanning operation performed by this scanner.
String
next
()
Finds and returns the next complete token from this scanner.
String
next
(
Pattern
pattern)
Returns the next string in the input that matches the specified pattern.
String
next
(
String
pattern)
Returns the next token if it matches the pattern constructed from the specified string.
BigDecimal
nextBigDecimal
()
Scans the next token of the input as a
BigDecimal
.
BigInteger
nextBigInteger
()
Scans the next token of the input as a
BigInteger
.
BigInteger
nextBigInteger
(int radix)
Scans the next token of the input as a
BigInteger
.
boolean
nextBoolean
()
Scans the next token of the input into a boolean value and returns that value.
byte
nextByte
()
Scans the next token of the input as a
byte
.
byte
nextByte
(int radix)
Scans the next token of the input as a
byte
.
double
nextDouble
()
Scans the next token of the input as a
double
.
float
nextFloat
()
Scans the next token of the input as a
float
.
int
nextInt
()
Scans the next token of the input as an
int
.
int
nextInt
(int radix)
Scans the next token of the input as an
int
.
String
nextLine
()
Advances this scanner past the current line and returns the input that was skipped.
long
nextLong
()
Scans the next token of the input as a
long
.
long
nextLong
(int radix)
Scans the next token of the input as a
long
.
short
nextShort
()
Scans the next token of the input as a
short
.
short
nextShort
(int radix)
Scans the next token of the input as a
short
.
int
radix
()
Returns this scanner's default radix.
void
remove
()
The remove operation is not supported by this implementation of Iterator.
Scanner
skip
(
Pattern
pattern)
Skips input that matches the specified pattern, ignoring delimiters.
Scanner
skip
(
String
pattern)
Skips input that matches a pattern constructed from the specified string.
String
toString
()
Returns the string representation of this Scanner.
Scanner
useDelimiter
(
Pattern
pattern)
Sets this scanner's delimiting pattern to the specified pattern.
Scanner
useDelimiter
(
String
pattern)
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
Scanner
useLocale
(
Locale
locale)
Sets this scanner's locale to the specified locale.
Scanner
useRadix
(int radix)
Sets this scanner's default radix to the specified radix.
Methods inherited from class java.lang.
Object
clone
,
equals
,
finalize
,
getClass
,
hashCode
,
notify
,
notifyAll
,
wait
,
wait
,
wait
Constructor
Method
Detail
Scanner
create
public 
public static 
Scanner
Scanner 
create (Readable source)
Constructs
Returns
a
new
Scanner that produces values scanned from the specified source.
Returns:
A scanner which parses text from the specified source
Scanner
create
public 
public static 
Scanner
Scanner 
create (InputStream source)
Constructs
Returns
a
new
Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform's default charset.
Returns:
A scanner which parses text from the specified source
Throws:
IOException
- if an I/O exception occurs
Scanner
create
public 
public static 
Scanner
Scanner 
create (InputStream source,
String charsetName)
Constructs
Returns
a
new
Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the specified charset.
Returns:
A scanner which parses text from the specified source
IOException
- if an I/O exception occurs
Scanner
create
public 
public static 
Scanner
Scanner 
create (File source)
throws FileNotFoundException
,

IOException
Constructs
Returns
a
new
Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset.
Returns:
A scanner which parses text from the specified source
IOException
- if an I/O exception occurs
Scanner
create
public 
public static 
Scanner
Scanner 
create (File source,
String charsetName)
throws FileNotFoundException
,

IOException
Constructs
Returns
a
new
Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the specified charset.
Returns:
A scanner which parses text from the specified source
IllegalArgumentException
IOException
- if an I/O exception occurs
UnsupportedEncodingException
- if the specified encoding is not found
Scanner
create
public 
public static 
Scanner
Scanner 
create (String source)
Scanner
create
public 
public static 
Scanner
Scanner 
create (ReadableByteChannel source)
Constructs
Returns
a
new
Scanner that produces values scanned from the specified channel. Bytes from the source are converted into characters using the underlying platform's default charset.
Scanner
create
public 
public static 
Scanner
Scanner 
create (ReadableByteChannel source,
String charsetName)
Constructs
Returns
a
new
Scanner that produces values scanned from the specified channel. Bytes from the source are converted into characters using the specified charset.
Returns:
AA scanner which parses text from the specified source
Method Detail
close
public void
close
()
Closes this scanner.
If this scanner has not yet been closed then if its underlying
readable
also implements the
Closeable
interface then the readable's
close
method will be invoked. If this scanner is already closed then invoking this method will have no effect.
Attempting to perform search operations after a scanner has been closed will result in an
IllegalStateException
.
ioException
close
public
IOException
public void
ioException 
close ()
Returns the IOException last thrown by this Scanner's underlying Readable. This method returns null if no such exception exists.
Closes this scanner.
If this scanner has not yet been closed then if its underlying
readable
also implements the
Closeable
interface then the readable's
close
method will be invoked. If this scanner is already closed then invoking this method will have no effect.
Attempting to perform search operations after a scanner has been closed will result in an
IllegalStateException
.
delimiter
ioException
public
Pattern
IOException
delimiter 
ioException ()
Returns the Pattern this Scanner is currently using to match delimiters.
Returns the IOException last thrown by this Scanner's underlying Readable. This method returns null if no such exception exists.
useDelimiter
delimiter
public
Scanner
Pattern
useDelimiter 
delimiter
(
Pattern
pattern) 
()
Sets this scanner's delimiting pattern to the specified pattern.
Returns the Pattern this Scanner is currently using to match delimiters.
useDelimiter
public ScanneruseDelimiter(
String
Pattern pattern)
a pattern constructed from
the specified
String.
pattern.
An invocation of this method of the form
useDelimiter(pattern)
behaves in exactly the same way as the invocation
hasDelimiter(Pattern.compile(pattern))
.
locale
useDelimiter
public
Locale
Scanner
locale 
useDelimiter
() 
( 
String
pattern)
Returns this scanner's locale.
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
A scanner's locale affects many elements of its default primitive matching regular expressions; see
localized numbers
above.
An invocation of this method of the form
useDelimiter(pattern)
behaves in exactly the same way as the invocation
hasDelimiter(Pattern.compile(pattern))
.
useLocale
locale
public
Scanner
Locale
useLocale 
locale
(
Locale
locale) 
()
Sets
Returns
this scanner's
locale to the specified
locale.
radix
useLocale
public int 
public 
Scanner
radix 
useLocale
() 
( 
Locale
locale)
Returns this scanner's default radix.
Sets this scanner's locale to the specified locale.
radix
locale
affects
many
elements of its default
number
primitive
matching regular expressions; see
localized numbers
above.
useRadix
radix
public
Scanner
public int
useRadix 
radix
(int radix) 
()
Sets
Returns
this scanner's default
radix to the specified
radix.
If the radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, then an IllegalArgumentException is thrown.
Parameters:
radix - The radix to use when scanning numbers
this scanner
Throws:
IllegalArgumentException
- if radix is out of range
the default radix of this scanner
match
useRadix
public
MatchResult
Scanner
match 
useRadix
() 
(int radix)
Returns the match result of the last scanning operation performed by this scanner. This method throws IllegalStateException if no match has been performed, or if the last match was not successful.
Sets this scanner's default radix to the specified radix.
The various nextmethods of Scanner make a match result available if they complete without throwing an exception. For instance, after an invocation of the
nextInt()
A scanner's radix affects elements of its default number matching regular expressions; see
localized numbers
method that returned an int, this method returns a MatchResult for the search of the
Integer
regular expression defined above. Similarly the
findInLine(java.lang.String)
,
findWithinHorizon(java.lang.String, int)
, and
skip(java.util.regex.Pattern)
methods will make a match available if they succeed.
above.
If the radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, then an IllegalArgumentException is thrown.
Parameters:
radix - The radix to use when scanning numbers
a match result for the last match operation
this scanner
IllegalStateException
IllegalArgumentException
- If no match result is available
- if radix is out of range
toString
match
public
String
MatchResult
toString 
match ()
Returns the match result of the last scanning operation performed by this scanner. This method throws IllegalStateException if no match has been performed, or if the last match was not successful.
Returns the string representation of this Scanner. The string representation of a Scanner contains information that may be useful for debugging. The exact format is unspecified.
The various nextmethods of Scanner make a match result available if they complete without throwing an exception. For instance, after an invocation of the
nextInt()
method that returned an int, this method returns a MatchResult for the search of the
Integer
regular expression defined above. Similarly the #find and
skip(java.util.regex.Pattern)
methods will make a match available if they succeed.
The string representation of this scanner
a match result for the last match operation
Throws:
IllegalStateException
- If no match result is available
hasNext
toString
public boolean 
public 
String
hasNext 
toString ()
Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
Returns the string representation of this Scanner. The string representation of a Scanner contains information that may be useful for debugging. The exact format is unspecified.
true if and only if this scanner has another token
Throws:
IllegalStateException
- if this scanner is closed
See Also:
Iterator
The string representation of this scanner
next
hasNext
public
String
public boolean
next 
hasNext ()
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of
hasNext()
returned true.
Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
the next token
true if and only if this scanner has another token
NoSuchElementException
- if no more tokens are available
remove
next
public void 
public 
String
remove 
next ()
The remove operation is not supported by this implementation of Iterator.
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of
hasNext()
returned true.
Returns:
the next token
UnsupportedOperationException
NoSuchElementException
- if this method is invoked.
- if no more tokens are available
IllegalStateException
- if this scanner is closed
hasNext
remove
public
boolean 
void
hasNext 
remove
(
String
pattern) 
()
Returns true if the next token matches the pattern constructed from the specified string. The scanner does not advance past any input.
The remove operation is not supported by this implementation of Iterator.
An invocation of this method of the form
hasNext(pattern)
behaves in exactly the same way as the invocation
hasNext(Pattern.compile(pattern))
.
Parameters:
Throws:
pattern - a string specifying the pattern to scan
UnsupportedOperationException
- if this method is invoked.
Returns:
See Also:
true if and only if this scanner has another token matching the specified pattern
Throws:
IllegalStateException
Iterator
- if this scanner is closed
next
hasNext
public
String
public boolean
next 
hasNext (String pattern)
true if
the next token
if it
matches the pattern constructed from the specified string.
If the match is successful, the
The
scanner
advances
does not advance
past
the input that matched the pattern.
any input.
next(pattern)
hasNext(pattern)
behaves in exactly the same way as the invocation
next(Pattern.compile(pattern))
hasNext(Pattern.compile(pattern))
.
the next token
true if and only if this scanner has another token matching the specified pattern
NoSuchElementException
- if no such tokens are available
hasNext
next
public boolean 
public 
String
hasNext 
next (
Pattern
String pattern)
Returns true if the next complete token matches the specified pattern. A complete token is prefixed and postfixed by input that matches the delimiter pattern. This method may block while waiting for input. The scanner does not advance past any input.
Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.
An invocation of this method of the form
next(pattern)
behaves in exactly the same way as the invocation
next(Pattern.compile(pattern))
.
a string specifying
the pattern to scan
for
true if and only if this scanner has another token matching the specified pattern
the next token
NoSuchElementException
- if no such tokens are available
next
hasNext
public
String
public boolean
next 
hasNext (Pattern pattern)
Returns the next string in the input that matches the specified pattern. This method may block while waiting for input to scan, even if a previous invocation of
hasNext(Pattern)
returned true. If the match is successful, the scanner advances past the input that matched the pattern.
Returns true if the next complete token matches the specified pattern. A complete token is prefixed and postfixed by input that matches the delimiter pattern. This method may block while waiting for input. The scanner does not advance past any input.
the next token
true if and only if this scanner has another token matching the specified pattern
NoSuchElementException
- if no more tokens are available
hasNextLine
next
public boolean 
public 
String
hasNextLine 
next
() 
( 
Pattern
pattern)
Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.
Returns the next string in the input that matches the specified pattern. This method may block while waiting for input to scan, even if a previous invocation of
hasNext(Pattern)
returned true. If the match is successful, the scanner advances past the input that matched the pattern.
Parameters:
pattern - the pattern to scan for
true if and only if this scanner has another line of input
the next token
NoSuchElementException
- if no more tokens are available
nextLine
public String nextLine()
not
found
findInLine
public String findInLine(String pattern)
findInLine
public String findInLine(Pattern pattern)
findWithinHorizon
public String findWithinHorizon(String pattern,
int horizon)
findWithinHorizon
public String findWithinHorizon(Pattern pattern,
int horizon)
skip
public Scanner skip(Pattern pattern)
skip
public Scanner skip(String pattern)
hasNextBoolean
public boolean hasNextBoolean()
nextBoolean
public boolean nextBoolean()
hasNextByte
public boolean hasNextByte()
hasNextByte
public boolean hasNextByte(int radix)
nextByte
public byte nextByte()
is
the
the
default radix of this scanner.
nextByte
public byte nextByte(int radix)
defined
defind
above then the token is converted into a
byte
value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via
Character.digit
, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to
Byte.parseByte
with the specified radix.
hasNextShort
public boolean hasNextShort()
hasNextShort
public boolean hasNextShort(int radix)
nextShort
public short nextShort()
is
the
the
default radix of this scanner.
nextShort
public short nextShort(int radix)
hasNextInt
public boolean hasNextInt()
hasNextInt
public boolean hasNextInt(int radix)
nextInt
public int nextInt()
is
the
the
default radix of this scanner.
nextInt
public int nextInt(int radix)
hasNextLong
public boolean hasNextLong()
hasNextLong
public boolean hasNextLong(int radix)
nextLong
public long nextLong()
is
the
the
default radix of this scanner.
nextLong
public long nextLong(int radix)
hasNextFloat
public boolean hasNextFloat()
nextFloat
public float nextFloat()
hasNextDouble
public boolean hasNextDouble()
nextDouble
public double nextDouble()
hasNextBigInteger
public boolean hasNextBigInteger()
hasNextBigInteger
public boolean hasNextBigInteger(int radix)
nextBigInteger
public BigInteger nextBigInteger()
is
the
the
default radix of this scanner.
nextBigInteger
public BigInteger nextBigInteger(int radix)
hasNextBigDecimal
public boolean hasNextBigDecimal()
nextBigDecimal
public BigDecimal nextBigDecimal()