Class Scanner
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterator<String>
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 Summary
Constructors Constructor Description Scanner(File src)Creates aScannerwith the specifiedFileas input.Scanner(File src, String charsetName)Creates aScannerwith the specifiedFileas input.Scanner(InputStream src)Creates aScanneron the specifiedInputStream.Scanner(InputStream src, String charsetName)Creates aScanneron the specifiedInputStream.Scanner(Readable src)Creates aScannerwith the specifiedReadableas input.Scanner(String src)Creates aScanneron the specified string.Scanner(ReadableByteChannel src)Creates aScannerwith the specifiedReadableByteChannelas input.Scanner(ReadableByteChannel src, String charsetName)Creates aScannerwith the specifiedReadableByteChannelas input. -
Method Summary
Modifier and Type Method Description voidclose()Closes thisScannerand the underlying input if the input implementsCloseable.Patterndelimiter()Returns the delimiterPatternin use by thisScanner.StringfindInLine(String pattern)Compiles the pattern string and tries to find a substring matching it in the input data.StringfindInLine(Pattern pattern)Tries to find the pattern in the input.StringfindWithinHorizon(String pattern, int horizon)Tries to find the pattern in the input between the current position and the specifiedhorizon.StringfindWithinHorizon(Pattern pattern, int horizon)Tries to find the pattern in the input between the current position and the specified horizon.booleanhasNext()Returns whether thisScannerhas one or more tokens remaining to parse.booleanhasNext(String pattern)Returnstrueif thisScannerhas one or more tokens remaining to parse and the next token matches a pattern compiled from the given string.booleanhasNext(Pattern pattern)Returns whether thisScannerhas one or more tokens remaining to parse and the next token matches the given pattern.booleanhasNextBigDecimal()Returns whether the next token can be translated into a validBigDecimal.booleanhasNextBigInteger()Returns whether the next token can be translated into a validBigIntegerin the default radix.booleanhasNextBigInteger(int radix)Returns whether the next token can be translated into a validBigIntegerin the specified radix.booleanhasNextBoolean()Returns whether the next token can be translated into a validbooleanvalue.booleanhasNextByte()Returns whether the next token can be translated into a validbytevalue in the default radix.booleanhasNextByte(int radix)Returns whether the next token can be translated into a validbytevalue in the specified radix.booleanhasNextDouble()Returns whether the next token translated into a validdoublevalue.booleanhasNextFloat()Returns whether the next token can be translated into a validfloatvalue.booleanhasNextInt()Returns whether the next token can be translated into a validintvalue in the default radix.booleanhasNextInt(int radix)Returns whether the next token can be translated into a validintvalue in the specified radix.booleanhasNextLine()Returns true if there is a line terminator in the input.booleanhasNextLong()Returns whether the next token can be translated into a validlongvalue in the default radix.booleanhasNextLong(int radix)Returns whether the next token can be translated into a validlongvalue in the specified radix.booleanhasNextShort()Returns whether the next token can be translated into a validshortvalue in the default radix.booleanhasNextShort(int radix)Returns whether the next token can be translated into a validshortvalue in the specified radix.IOExceptionioException()Returns the lastIOExceptionthat was raised while reading from the underlying input, ornullif none was thrown.Localelocale()Returns theLocaleof thisScanner.MatchResultmatch()Returns the result of the last matching operation.Stringnext()Returns the next token.Stringnext(String pattern)Returns the next token if it matches the specified pattern.Stringnext(Pattern pattern)Returns the next token if it matches the specified pattern.BigDecimalnextBigDecimal()Returns the next token as aBigDecimal.BigIntegernextBigInteger()Returns the next token as aBigIntegerin the current radix.BigIntegernextBigInteger(int radix)Returns the next token as aBigIntegerwith the specified radix.booleannextBoolean()Returns the next token as aboolean.bytenextByte()Returns the next token as abytein the current radix.bytenextByte(int radix)Returns the next token as abytewith the specified radix.doublenextDouble()Returns the next token as adouble.floatnextFloat()Returns the next token as afloat.intnextInt()Returns the next token as anintin the current radix.intnextInt(int radix)Returns the next token as anintwith the specified radix.StringnextLine()Returns the skipped input and advances theScannerto the beginning of the next line.longnextLong()Returns the next token as alongin the current radix.longnextLong(int radix)Returns the next token as alongwith the specified radix.shortnextShort()Returns the next token as ashortin the current radix.shortnextShort(int radix)Returns the next token as ashortwith the specified radix.intradix()Return the radix of thisScanner.voidremove()Remove is not a supported operation onScanner.Scannerreset()Resets this scanner's delimiter, locale, and radix.Scannerskip(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.Scannerskip(Pattern pattern)Tries to use specified pattern to match input starting from the current position.StringtoString()Returns a string representation of thisScanner.ScanneruseDelimiter(String pattern)Sets the delimiting pattern of thisScannerwith a pattern compiled from the supplied string value.ScanneruseDelimiter(Pattern pattern)Sets the delimiting pattern of thisScanner.ScanneruseLocale(Locale l)Sets theLocaleof thisScannerto a specifiedLocale.ScanneruseRadix(int radix)Sets the radix of thisScannerto the specified radix.
-
Constructor Details
-
Scanner
Creates aScannerwith the specifiedFileas input. The default charset is applied when reading the file.- Parameters:
src- the file to be scanned.- Throws:
FileNotFoundException- if the specified file does not exist.
-
Scanner
Creates aScannerwith the specifiedFileas input. The specified charset is applied when reading the file.- Parameters:
src- the file to be scanned.charsetName- 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
Creates aScanneron the specified string.- Parameters:
src- the string to be scanned.
-
Scanner
Creates aScanneron the specifiedInputStream. The default charset is applied when decoding the input.- Parameters:
src- theInputStreamto be scanned.
-
Scanner
Creates aScanneron the specifiedInputStream. The specified charset is applied when decoding the input.- Parameters:
src- theInputStreamto be scanned.charsetName- the encoding type of theInputStream.- Throws:
IllegalArgumentException- if the specified character set is not found.
-
Scanner
Creates aScannerwith the specifiedReadableas input.- Parameters:
src- theReadableto be scanned.
-
Scanner
Creates aScannerwith the specifiedReadableByteChannelas input. The default charset is applied when decoding the input.- Parameters:
src- theReadableByteChannelto be scanned.
-
Scanner
Creates aScannerwith the specifiedReadableByteChannelas input. The specified charset is applied when decoding the input.- Parameters:
src- theReadableByteChannelto be scanned.charsetName- the encoding type of the content.- Throws:
IllegalArgumentException- if the specified character set is not found.
-
-
Method Details
-
close
public void close()Closes thisScannerand the underlying input if the input implementsCloseable. If theScannerhas been closed, this method will have no effect. Any scanning operation called after calling this method will throw anIllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- See Also:
Closeable
-
delimiter
Returns the delimiterPatternin use by thisScanner.- Returns:
- the delimiter
Patternin use by thisScanner.
-
findInLine
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 theScannerwill advance to the end of the matched string. Otherwise,nullwill be returned and theScannerwill not advance. When waiting for input, theScannermay be blocked. All the input may be cached if no line terminator exists in the buffer.- Parameters:
pattern- the pattern to find in the input.- Returns:
- the matched string or
nullif the pattern is not found before the next line terminator. - Throws:
IllegalStateException- if theScanneris closed.
-
findInLine
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 invokingfindInLine(Pattern.compile(pattern)).- Parameters:
pattern- a string used to construct a pattern which is in turn used to match a substring of the input data.- Returns:
- the matched string or
nullif the pattern is not found before the next line terminator. - Throws:
IllegalStateException- if theScanneris closed.- See Also:
findInLine(Pattern)
-
findWithinHorizon
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 theScannerwill advance to the end of the matched string. Otherwise, null will be returned andScannerwill not advance. When waiting for input, theScannermay be blocked.The
Scanner's search will never go more thanhorizoncode points from current position. The position ofhorizondoes 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 returnnull, whilefindWithinHorizon(Pattern.compile("\\p{Digit}{3}"), 3)will return"123".horizonis treated as a transparent, non-anchoring bound. (refer toMatcher.useTransparentBounds(boolean)andMatcher.useAnchoringBounds(boolean))A
horizonwhose 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- the pattern used to scan.horizon- the search limit.- Returns:
- the matched string or
nullif the pattern is not found within the specifiedhorizon. - Throws:
IllegalStateException- if theScanneris closed.IllegalArgumentException- ifhorizonis less than zero.
-
findWithinHorizon
Tries to find the pattern in the input between the current position and the specifiedhorizon. Delimiters are ignored. This call is the same as invokingfindWithinHorizon(Pattern.compile(pattern)).- Parameters:
pattern- the pattern used to scan.horizon- the search limit.- Returns:
- the matched string, or
nullif the pattern is not found within the specified horizon. - Throws:
IllegalStateException- if theScanneris closed.IllegalArgumentException- ifhorizonis less than zero.- See Also:
findWithinHorizon(Pattern, int)
-
hasNext
public boolean hasNext()Returns whether thisScannerhas one or more tokens remaining to parse. This method will block if the data is still being read.- Specified by:
hasNextin interfaceIterator<String>- Returns:
trueif thisScannerhas one or more tokens remaining, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.- See Also:
Iterator.next()
-
hasNext
Returns whether thisScannerhas 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- the pattern to check for.- Returns:
trueif thisScannerhas more tokens and the next token matches the pattern,falseotherwise.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNext
Returnstrueif thisScannerhas 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 tohasNext(Pattern.compile(pattern)).- Parameters:
pattern- the string specifying the pattern to scan for- Returns:
trueif the specified pattern matches thisScanner's next token,falseotherwise.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextBigDecimal
public boolean hasNextBigDecimal()Returns whether the next token can be translated into a validBigDecimal.- Returns:
trueif the next token can be translated into a validBigDecimal, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextBigInteger
public boolean hasNextBigInteger()Returns whether the next token can be translated into a validBigIntegerin the default radix.- Returns:
trueif the next token can be translated into a validBigInteger, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextBigInteger
public boolean hasNextBigInteger(int radix)Returns whether the next token can be translated into a validBigIntegerin the specified radix.- Parameters:
radix- the radix used to translate the token into aBigInteger.- Returns:
trueif the next token can be translated into a validBigInteger, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextBoolean
public boolean hasNextBoolean()Returns whether the next token can be translated into a validbooleanvalue.- Returns:
trueif the next token can be translated into a validbooleanvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextByte
public boolean hasNextByte()Returns whether the next token can be translated into a validbytevalue in the default radix.- Returns:
trueif the next token can be translated into a validbytevalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextByte
public boolean hasNextByte(int radix)Returns whether the next token can be translated into a validbytevalue in the specified radix.- Parameters:
radix- the radix used to translate the token into abytevalue- Returns:
trueif the next token can be translated into a validbytevalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextDouble
public boolean hasNextDouble()Returns whether the next token translated into a validdoublevalue.- Returns:
trueif the next token can be translated into a validdoublevalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextFloat
public boolean hasNextFloat()Returns whether the next token can be translated into a validfloatvalue.- Returns:
trueif the next token can be translated into a validfloatvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextInt
public boolean hasNextInt()Returns whether the next token can be translated into a validintvalue in the default radix.- Returns:
trueif the next token can be translated into a validintvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed,
-
hasNextInt
public boolean hasNextInt(int radix)Returns whether the next token can be translated into a validintvalue in the specified radix.- Parameters:
radix- the radix used to translate the token into anintvalue.- Returns:
trueif the next token in thisScanner's input can be translated into a validintvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextLine
public boolean hasNextLine()Returns true if there is a line terminator in the input. This method may block.- Throws:
IllegalStateException- if thisScanneris closed.
-
hasNextLong
public boolean hasNextLong()Returns whether the next token can be translated into a validlongvalue in the default radix.- Returns:
trueif the next token can be translated into a validlongvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextLong
public boolean hasNextLong(int radix)Returns whether the next token can be translated into a validlongvalue in the specified radix.- Parameters:
radix- the radix used to translate the token into alongvalue.- Returns:
trueif the next token can be translated into a validlongvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextShort
public boolean hasNextShort()Returns whether the next token can be translated into a validshortvalue in the default radix.- Returns:
trueif the next token can be translated into a validshortvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
hasNextShort
public boolean hasNextShort(int radix)Returns whether the next token can be translated into a validshortvalue in the specified radix.- Parameters:
radix- the radix used to translate the token into ashortvalue.- Returns:
trueif the next token can be translated into a validshortvalue, otherwisefalse.- Throws:
IllegalStateException- if theScannerhas been closed.
-
ioException
Returns the lastIOExceptionthat was raised while reading from the underlying input, ornullif none was thrown. -
locale
Returns theLocaleof thisScanner. -
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:
- 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
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.- Specified by:
nextin interfaceIterator<String>- Returns:
- the next complete token.
- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.- See Also:
Iterator.hasNext()
-
next
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- the specified pattern to scan.- Returns:
- the next token.
- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token does not match the pattern given.
-
next
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 tonext(Pattern.compile(pattern)).- Parameters:
pattern- the string specifying the pattern to scan for.- Returns:
- the next token.
- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token does not match the pattern given.
-
nextBigDecimal
Returns the next token as aBigDecimal. This method will block if input is being read. If the next token can be translated into aBigDecimalthe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting string is passed toBigDecimal(String).- Returns:
- the next token as a
BigDecimal. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validBigDecimal.
-
nextBigInteger
Returns the next token as aBigIntegerin the current radix. This method may block for more input.- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validBigInteger.
-
nextBigInteger
Returns the next token as aBigIntegerwith the specified radix. This method will block if input is being read. If the next token can be translated into aBigIntegerthe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toBigInteger(String, int)} with the specified radix.- Parameters:
radix- the radix used to translate the token into aBigInteger.- Returns:
- the next token as a
BigInteger - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validBigInteger.
-
nextBoolean
public boolean nextBoolean()Returns the next token as aboolean. This method will block if input is being read.- Returns:
- the next token as a
boolean. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validbooleanvalue.
-
nextByte
public byte nextByte()Returns the next token as abytein the current radix. This method may block for more input.- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validbytevalue.
-
nextByte
public byte nextByte(int radix)Returns the next token as abytewith the specified radix. Will block if input is being read. If the next token can be translated into abytethe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toByte.parseByte(String, int)} with the specified radix.- Parameters:
radix- the radix used to translate the token intobytevalue.- Returns:
- the next token as a
byte. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validbytevalue.
-
nextDouble
public double nextDouble()Returns the next token as adouble. This method will block if input is being read. If the next token can be translated into adoublethe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toDouble.parseDouble(String)}. If the token matches the localized NaN or infinity strings, it is also passed toDouble.parseDouble(String)}.- Returns:
- the next token as a
double. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validdoublevalue.
-
nextFloat
public float nextFloat()Returns the next token as afloat. This method will block if input is being read. If the next token can be translated into afloatthe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toFloat.parseFloat(String)}.If the token matches the localized NaN or infinity strings, it is also passed toFloat.parseFloat(String)}.- Returns:
- the next token as a
float. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validfloatvalue.
-
nextInt
public int nextInt()Returns the next token as anintin the current radix. This method may block for more input.- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validintvalue.
-
nextInt
public int nextInt(int radix)Returns the next token as anintwith the specified radix. This method will block if input is being read. If the next token can be translated into anintthe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toInteger.parseInt(String, int)with the specified radix.- Parameters:
radix- the radix used to translate the token into anintvalue.- Returns:
- the next token as an
int. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validintvalue.
-
nextLine
Returns the skipped input and advances theScannerto 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, aNoSuchElementExceptionwill be thrown.- Returns:
- the skipped line.
- Throws:
IllegalStateException- if theScanneris closed.NoSuchElementException- if no line can be found, e.g. when input is an empty string.
-
nextLong
public long nextLong()Returns the next token as alongin the current radix. This method may block for more input.- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validlongvalue.
-
nextLong
public long nextLong(int radix)Returns the next token as alongwith the specified radix. This method will block if input is being read. If the next token can be translated into alongthe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toLong.parseLong(String, int)} with the specified radix.- Parameters:
radix- the radix used to translate the token into alongvalue.- Returns:
- the next token as a
long. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validlongvalue.
-
nextShort
public short nextShort()Returns the next token as ashortin the current radix. This method may block for more input.- Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validshortvalue.
-
nextShort
public short nextShort(int radix)Returns the next token as ashortwith the specified radix. This method will block if input is being read. If the next token can be translated into ashortthe following is done: AllLocale-specific prefixes, group separators, andLocale-specific suffixes are removed. Then non-ASCII digits are mapped into ASCII digits viaCharacter.digit(char, int), and a negative sign (-) is added if theLocale-specific negative prefix or suffix was present. Finally the resulting String is passed toShort.parseShort(String, int)} with the specified radix.- Parameters:
radix- the radix used to translate the token intoshortvalue.- Returns:
- the next token as a
short. - Throws:
IllegalStateException- if thisScannerhas been closed.NoSuchElementException- if input has been exhausted.InputMismatchException- if the next token can not be translated into a validshortvalue.
-
radix
public int radix()Return the radix of thisScanner.- Returns:
- the radix of this
Scanner
-
skip
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, aNoSuchElementExceptionwill be thrown. Patterns that can match a lot of input may cause theScannerto read in a large amount of input.- Parameters:
pattern- used to skip over input.- Returns:
- the
Scanneritself. - Throws:
IllegalStateException- if theScanneris closed.NoSuchElementException- if the specified pattern match fails.
-
skip
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 invokeskip(Pattern.compile(pattern)).- Parameters:
pattern- the string used to construct a pattern which in turn is used to match input.- Returns:
- the
Scanneritself. - Throws:
IllegalStateException- if theScanneris closed.
-
toString
Returns a string representation of thisScanner. The information returned may be helpful for debugging. The format of the string is unspecified. -
useDelimiter
Sets the delimiting pattern of thisScanner.- Parameters:
pattern- the delimiting pattern to use.- Returns:
- this
Scanner.
-
useDelimiter
Sets the delimiting pattern of thisScannerwith a pattern compiled from the supplied string value.- Parameters:
pattern- a string from which aPatterncan be compiled.- Returns:
- this
Scanner.
-
useLocale
Sets theLocaleof thisScannerto a specifiedLocale.- Parameters:
l- the specifiedLocaleto use.- Returns:
- this
Scanner.
-
useRadix
Sets the radix of thisScannerto the specified radix.- Parameters:
radix- the specified radix to use.- Returns:
- this
Scanner.
-
remove
public void remove()Remove is not a supported operation onScanner.- Specified by:
removein interfaceIterator<String>- Throws:
UnsupportedOperationException- if this method is invoked.
-
reset
Resets this scanner's delimiter, locale, and radix.- Returns:
- this scanner
- Since:
- 1.6
-