Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Scanner

public final class Scanner
extends Object implements Closeable, Iterator<String>

java.lang.Object
   ↳ java.util.Scanner


A parser that parses a text string of primitive types and strings with the help of regular expressions. This class is not as useful as it might seem. It's very inefficient for communicating between machines; you should use JSON, protobufs, or even XML for that. Very simple uses might get away with split(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.

Summary

Public constructors

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(String src)

Creates a Scanner on the specified string.

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.

Public methods

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(String pattern)

Compiles the pattern string and tries to find a substring matching it in the input data.

String findInLine(Pattern pattern)

Tries to find the pattern in the input.

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(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 hasNext(Pattern pattern)

Returns whether this Scanner has one or more tokens remaining to parse and the next token matches the given pattern.

boolean hasNextBigDecimal()

Returns whether the next token can be translated into a valid BigDecimal.

boolean hasNextBigInteger(int radix)

Returns whether the next token can be translated into a valid BigInteger in the specified radix.

boolean hasNextBigInteger()

Returns whether the next token can be translated into a valid BigInteger in the default 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(int radix)

Returns whether the next token can be translated into a valid int value in the specified radix.

boolean hasNextInt()

Returns whether the next token can be translated into a valid int value in the default radix.

boolean hasNextLine()

Returns true if there is a line terminator in the input.

boolean hasNextLong(int radix)

Returns whether the next token can be translated into a valid long value in the specified radix.

boolean hasNextLong()

Returns whether the next token can be translated into a valid long 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.

boolean hasNextShort()

Returns whether the next token can be translated into a valid short value in the default 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(int radix)

Returns the next token as a BigInteger with the specified radix.

BigInteger nextBigInteger()

Returns the next token as a BigInteger in the current 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(int radix)

Returns the next token as an int with the specified radix.

int nextInt()

Returns the next token as an int in the current radix.

String nextLine()

Returns the skipped input and advances the Scanner to the beginning of the next line.

long nextLong(int radix)

Returns the next token as a long with the specified radix.

long nextLong()

Returns the next token as a long in the current radix.

short nextShort(int radix)

Returns the next token as a short with the specified radix.

short nextShort()

Returns the next token as a short in the current 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(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.

Scanner skip(Pattern pattern)

Tries to use specified pattern to match input starting from the current position.

String toString()

Returns a string representation of this Scanner.

Scanner useDelimiter(String pattern)

Sets the delimiting pattern of this Scanner with a pattern compiled from the supplied string value.

Scanner useDelimiter(Pattern pattern)

Sets the delimiting pattern of this Scanner.

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.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.util.Iterator
From interface java.lang.AutoCloseable

Public constructors

Scanner

Added in API level 1
Scanner (File src)

Creates a Scanner with the specified File as input. The default charset is applied when reading the file.

Parameters
src File: the file to be scanned.
Throws
FileNotFoundException if the specified file does not exist.

Scanner

Added in API level 1
Scanner (File src, 
                String charsetName)

Creates a Scanner with the specified File as input. The specified charset is applied when reading the file.

Parameters
src File: the file to be scanned.
charsetName String: the name of the encoding type of the file.
Throws
FileNotFoundException if the specified file does not exist.
IllegalArgumentException if the specified coding does not exist.

Scanner

Added in API level 1
Scanner (String src)

Creates a Scanner on the specified string.

Parameters
src String: the string to be scanned.

Scanner

Added in API level 1
Scanner (InputStream src)

Creates a Scanner on the specified InputStream. The default charset is applied when decoding the input.

Parameters
src InputStream: the InputStream to be scanned.

Scanner

Added in API level 1
Scanner (InputStream src, 
                String charsetName)

Creates a Scanner on the specified InputStream. The specified charset is applied when decoding the input.

Parameters
src InputStream: the InputStream to be scanned.
charsetName String: the encoding type of the InputStream.
Throws
IllegalArgumentException if the specified character set is not found.

Scanner

Added in API level 1
Scanner (Readable src)

Creates a Scanner with the specified Readable as input.

Parameters
src Readable: the Readable to be scanned.

Scanner

Added in API level 1
Scanner (ReadableByteChannel src)

Creates a Scanner with the specified ReadableByteChannel as input. The default charset is applied when decoding the input.

Parameters
src ReadableByteChannel: the ReadableByteChannel to be scanned.

Scanner

Added in API level 1
Scanner (ReadableByteChannel src, 
                String charsetName)

Creates a Scanner with the specified ReadableByteChannel as input. The specified charset is applied when decoding the input.

Parameters
src ReadableByteChannel: the ReadableByteChannel to be scanned.
charsetName String: the encoding type of the content.
Throws
IllegalArgumentException if the specified character set is not found.

Public methods

close

Added in API level 1
void close ()

Closes this 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.

See also:

delimiter

Added in API level 1
Pattern delimiter ()

Returns the delimiter Pattern in use by this Scanner.

Returns
Pattern the delimiter Pattern in use by this Scanner.

findInLine

Added in API level 1
String findInLine (String pattern)

Compiles the pattern string and tries to find a substring matching it in the input data. The delimiter will be ignored. This is the same as invoking findInLine(Pattern.compile(pattern)).

Parameters
pattern String: a string used to construct a pattern which is in turn used to match a substring of the input data.
Returns
String the matched string or null if the pattern is not found before the next line terminator.
Throws
IllegalStateException if the Scanner is closed.

See also:

findInLine

Added in API level 1
String findInLine (Pattern pattern)

Tries to find the pattern in the input. Delimiters are ignored. If the pattern is found before line terminator, the matched string will be returned, and the 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.

Parameters
pattern Pattern: the pattern to find in the input.
Returns
String the matched string or null if the pattern is not found before the next line terminator.
Throws
IllegalStateException if the Scanner is closed.

findWithinHorizon

Added in API level 1
String findWithinHorizon (Pattern pattern, 
                int horizon)

Tries to find the pattern in the input between the current position and the specified horizon. Delimiters are ignored. If the pattern is found, the matched string will be returned, and the 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 useTransparentBounds(boolean) and 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.

Parameters
pattern Pattern: the pattern used to scan.
horizon int: the search limit.
Returns
String the matched string or null if the pattern is not found within the specified horizon.
Throws
IllegalStateException if the Scanner is closed.
IllegalArgumentException if horizon is less than zero.

findWithinHorizon

Added in API level 1
String findWithinHorizon (String pattern, 
                int horizon)

Tries to find the pattern in the input between the current position and the specified horizon. Delimiters are ignored. This call is the same as invoking findWithinHorizon(Pattern.compile(pattern)).

Parameters
pattern String: the pattern used to scan.
horizon int: the search limit.
Returns
String the matched string, or null if the pattern is not found within the specified horizon.
Throws
IllegalStateException if the Scanner is closed.
IllegalArgumentException if horizon is less than zero.

See also:

hasNext

Added in API level 1
boolean hasNext ()

Returns whether this Scanner has one or more tokens remaining to parse. This method will block if the data is still being read.

Returns
boolean true if this Scanner has one or more tokens remaining, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNext

Added in API level 1
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. This method will block if the data is still being read. This call is equivalent to hasNext(Pattern.compile(pattern)).

Parameters
pattern String: the string specifying the pattern to scan for
Returns
boolean true if the specified pattern matches this Scanner's next token, false otherwise.
Throws
IllegalStateException if the Scanner has been closed.

hasNext

Added in API level 1
boolean hasNext (Pattern pattern)

Returns whether this 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.

Parameters
pattern Pattern: the pattern to check for.
Returns
boolean true if this Scanner has more tokens and the next token matches the pattern, false otherwise.
Throws
IllegalStateException if the Scanner has been closed.

hasNextBigDecimal

Added in API level 1
boolean hasNextBigDecimal ()

Returns whether the next token can be translated into a valid BigDecimal.

Returns
boolean true if the next token can be translated into a valid BigDecimal, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextBigInteger

Added in API level 1
boolean hasNextBigInteger (int radix)

Returns whether the next token can be translated into a valid BigInteger in the specified radix.

Parameters
radix int: the radix used to translate the token into a BigInteger.
Returns
boolean true if the next token can be translated into a valid BigInteger, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextBigInteger

Added in API level 1
boolean hasNextBigInteger ()

Returns whether the next token can be translated into a valid BigInteger in the default radix.

Returns
boolean true if the next token can be translated into a valid BigInteger, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextBoolean

Added in API level 1
boolean hasNextBoolean ()

Returns whether the next token can be translated into a valid boolean value.

Returns
boolean true if the next token can be translated into a valid boolean value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextByte

Added in API level 1
boolean hasNextByte ()

Returns whether the next token can be translated into a valid byte value in the default radix.

Returns
boolean true if the next token can be translated into a valid byte value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextByte

Added in API level 1
boolean hasNextByte (int radix)

Returns whether the next token can be translated into a valid byte value in the specified radix.

Parameters
radix int: the radix used to translate the token into a byte value
Returns
boolean true if the next token can be translated into a valid byte value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextDouble

Added in API level 1
boolean hasNextDouble ()

Returns whether the next token translated into a valid double value.

Returns
boolean true if the next token can be translated into a valid double value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextFloat

Added in API level 1
boolean hasNextFloat ()

Returns whether the next token can be translated into a valid float value.

Returns
boolean true if the next token can be translated into a valid float value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextInt

Added in API level 1
boolean hasNextInt (int radix)

Returns whether the next token can be translated into a valid int value in the specified radix.

Parameters
radix int: the radix used to translate the token into an int value.
Returns
boolean true if the next token in this Scanner's input can be translated into a valid int value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextInt

Added in API level 1
boolean hasNextInt ()

Returns whether the next token can be translated into a valid int value in the default radix.

Returns
boolean true if the next token can be translated into a valid int value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed,

hasNextLine

Added in API level 1
boolean hasNextLine ()

Returns true if there is a line terminator in the input. This method may block.

Returns
boolean
Throws
IllegalStateException if this Scanner is closed.

hasNextLong

Added in API level 1
boolean hasNextLong (int radix)

Returns whether the next token can be translated into a valid long value in the specified radix.

Parameters
radix int: the radix used to translate the token into a long value.
Returns
boolean true if the next token can be translated into a valid long value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextLong

Added in API level 1
boolean hasNextLong ()

Returns whether the next token can be translated into a valid long value in the default radix.

Returns
boolean true if the next token can be translated into a valid long value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextShort

Added in API level 1
boolean hasNextShort (int radix)

Returns whether the next token can be translated into a valid short value in the specified radix.

Parameters
radix int: the radix used to translate the token into a short value.
Returns
boolean true if the next token can be translated into a valid short value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

hasNextShort

Added in API level 1
boolean hasNextShort ()

Returns whether the next token can be translated into a valid short value in the default radix.

Returns
boolean true if the next token can be translated into a valid short value, otherwise false.
Throws
IllegalStateException if the Scanner has been closed.

ioException

Added in API level 1
IOException ioException ()

Returns the last IOException that was raised while reading from the underlying input, or null if none was thrown.

Returns
IOException

locale

Added in API level 1
Locale locale ()

Returns the Locale of this Scanner.

Returns
Locale

match

Added in API level 1
MatchResult match ()

Returns the result of the last matching operation.

The next* and find* methods return the match result in the case of a successful match.

Returns
MatchResult the match result of the last successful match operation
Throws
IllegalStateException if the match result is not available, of if the last match was not successful.

next

Added in API level 1
String next ()

Returns the next token. The token will be both prefixed and suffixed by the delimiter that is currently being used (or a string that matches the delimiter pattern). This method will block if input is being read.

Returns
String the next complete token.
Throws
IllegalStateException if this Scanner has been closed.
NoSuchElementException if input has been exhausted.

next

Added in API level 1
String next (Pattern pattern)

Returns the next token if it matches the specified pattern. The token will be both prefixed and suffixed by the delimiter that is currently being used (or a string that matches the delimiter pattern). This method will block if input is being read.

Parameters
pattern Pattern: the specified pattern to scan.
Returns
String the next token.
Throws
IllegalStateException if this Scanner has been closed.
NoSuchElementException if input has been exhausted.
InputMismatchException if the next token does not match the pattern given.

next

Added in API level 1
String next (String pattern)

Returns the next token if it matches the specified pattern. The token will be both prefixed and suffixed by the delimiter that is currently being used (or a string that matches the delimiter pattern). This method will block if input is being read. Calling this method is equivalent to next(Pattern.compile(pattern)).

Parameters
pattern String: the string specifying the pattern to scan for.
Returns
String the next token.
Throws
IllegalStateException if this Scanner has been closed.
NoSuchElementException if input has been exhausted.
InputMismatchException if the next token does not match the pattern given.

nextBigDecimal

Added in API level 1
BigDecimal nextBigDecimal ()

Returns the next token as a 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 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) .

Returns
BigDecimal the next token as a BigDecimal.
Throws
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.

nextBigInteger

Added in API level 1
BigInteger nextBigInteger (int radix)

Returns the next token as a 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 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.

Parameters
radix int: the radix used to translate the token into a BigInteger.
Returns
BigInteger the next token as a BigInteger
Throws
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.

nextBigInteger

Added in API level 1
BigInteger nextBigInteger ()

Returns the next token as a BigInteger in the current radix. This method may block for more input.

Returns
BigInteger
Throws
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.

nextBoolean

Added in API level 1
boolean nextBoolean ()

Returns the next token as a boolean. This method will block if input is being read.

Returns
boolean the next token as a boolean.
Throws
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.

nextByte

Added in API level 1
byte nextByte ()

Returns the next token as a byte in the current radix. This method may block for more input.

Returns
byte
Throws
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.

nextByte

Added in API level 1
byte nextByte (int radix)

Returns the next token as a 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 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 parseByte(String, int)} with the specified radix.

Parameters
radix int: the radix used to translate the token into byte value.
Returns
byte the next token as a byte.
Throws
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.

nextDouble

Added in API level 1
double nextDouble ()

Returns the next token as a 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 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 parseDouble(String)}. If the token matches the localized NaN or infinity strings, it is also passed to parseDouble(String)}.

Returns
double the next token as a double.
Throws
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.

nextFloat

Added in API level 1
float nextFloat ()

Returns the next token as a 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 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 parseFloat(String)}.If the token matches the localized NaN or infinity strings, it is also passed to parseFloat(String)}.

Returns
float the next token as a float.
Throws
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.

nextInt

Added in API level 1
int nextInt (int radix)

Returns the next token as an 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 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 parseInt(String, int) with the specified radix.

Parameters
radix int: the radix used to translate the token into an int value.
Returns
int the next token as an int.
Throws
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.

nextInt

Added in API level 1
int nextInt ()

Returns the next token as an int in the current radix. This method may block for more input.

Returns
int
Throws
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.

nextLine

Added in API level 1
String nextLine ()

Returns the skipped input and advances the 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.

Returns
String the skipped line.
Throws
IllegalStateException if the Scanner is closed.
NoSuchElementException if no line can be found, e.g. when input is an empty string.

nextLong

Added in API level 1
long nextLong (int radix)

Returns the next token as a 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 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 parseLong(String, int)} with the specified radix.

Parameters
radix int: the radix used to translate the token into a long value.
Returns
long the next token as a long.
Throws
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.

nextLong

Added in API level 1
long nextLong ()

Returns the next token as a long in the current radix. This method may block for more input.

Returns
long
Throws
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.

nextShort

Added in API level 1
short nextShort (int radix)

Returns the next token as a 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 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 parseShort(String, int)} with the specified radix.

Parameters
radix int: the radix used to translate the token into short value.
Returns
short the next token as a short.
Throws
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.

nextShort

Added in API level 1
short nextShort ()

Returns the next token as a short in the current radix. This method may block for more input.

Returns
short
Throws
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.

radix

Added in API level 1
int radix ()

Return the radix of this Scanner.

Returns
int the radix of this Scanner

remove

Added in API level 1
void remove ()

Remove is not a supported operation on Scanner.

Throws
UnsupportedOperationException if this method is invoked.

reset

Added in API level 9
Scanner reset ()

Resets this scanner's delimiter, locale, and radix.

Returns
Scanner this scanner

skip

Added in API level 1
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. The delimiter will be ignored. This call is the same as invoke skip(Pattern.compile(pattern)).

Parameters
pattern String: the string used to construct a pattern which in turn is used to match input.
Returns
Scanner the Scanner itself.
Throws
IllegalStateException if the Scanner is closed.

skip

Added in API level 1
Scanner skip (Pattern pattern)

Tries to use specified pattern to match input starting from the current position. The delimiter will be ignored. If a match is found, the matched input will be skipped. If an anchored match of the specified pattern succeeds, the corresponding input will also be skipped. Otherwise, a NoSuchElementException will be thrown. Patterns that can match a lot of input may cause the Scanner to read in a large amount of input.

Parameters
pattern Pattern: used to skip over input.
Returns
Scanner the Scanner itself.
Throws
IllegalStateException if the Scanner is closed.
NoSuchElementException if the specified pattern match fails.

toString

Added in API level 1
String toString ()

Returns a string representation of this Scanner. The information returned may be helpful for debugging. The format of the string is unspecified.

Returns
String a string representation of this Scanner.

useDelimiter

Added in API level 1
Scanner useDelimiter (String pattern)

Sets the delimiting pattern of this Scanner with a pattern compiled from the supplied string value.

Parameters
pattern String: a string from which a Pattern can be compiled.
Returns
Scanner this Scanner.

useDelimiter

Added in API level 1
Scanner useDelimiter (Pattern pattern)

Sets the delimiting pattern of this Scanner.

Parameters
pattern Pattern: the delimiting pattern to use.
Returns
Scanner this Scanner.

useLocale

Added in API level 1
Scanner useLocale (Locale l)

Sets the Locale of this Scanner to a specified Locale.

Parameters
l Locale: the specified Locale to use.
Returns
Scanner this Scanner.

useRadix

Added in API level 1
Scanner useRadix (int radix)

Sets the radix of this Scanner to the specified radix.

Parameters
radix int: the specified radix to use.
Returns
Scanner this Scanner.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.