public final classScannerextendsObjectimplementsIterator<String>,Closeable
AScanner 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 variousnext methods.
For example, this code allows a user to read a number fromSystem.in:
Scanner sc = new Scanner(System.in); int i = sc.nextInt();
As another example, this code allowslong types to be assigned from entries in a filemyNumbers:
Scanner sc = new Scanner(new File("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*"); 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); 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();Thedefault whitespace delimiter used by a scanner is as recognized byCharacter.isWhitespace. Thereset() method will reset the value of the scanner's delimiter to the default whitespace delimiter regardless of whether it was previously changed.
A scanning operation may block waiting for input.
Thenext() andhasNext() methods and their primitive-type companion methods (such asnextInt() andhasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. BothhasNext andnext methods may block waiting for further input. Whether ahasNext method blocks has no connection to whether or not its associatednext method will block.
ThefindInLine(java.lang.String),findWithinHorizon(java.lang.String, int), andskip(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 anInputMismatchException, 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+" will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern"\\s" could return empty tokens since it only passes one space at a time.
A scanner can read text from any object which implements theReadable interface. If an invocation of the underlying readable'sReadable.read(java.nio.CharBuffer) method throws anIOException then the scanner assumes that the end of the input has been reached. The most recentIOException thrown by the underlying readable can be retrieved via theioException() method.
When aScanner is closed, it will close its input source if the source implements theCloseable interface.
AScanner is not safe for multithreaded use without external synchronization.
Unless otherwise mentioned, passing anull parameter into any method of aScanner will cause aNullPointerException to be thrown.
A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the 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'sinitial localeis the value returned by the The localized formats are defined in terms of the following parameters, which for a particular locale are taken from that locale's 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).useRadix(int) method. Thereset() method will reset the value of the scanner's radix to10 regardless of whether it was previously changed. Localized numbers
Locale.getDefault() method; it may be changed via theuseLocale(java.util.Locale) method. Thereset() method will reset the value of the scanner's locale to the initial locale regardless of whether it was previously changed.DecimalFormat object,df, and its andDecimalFormatSymbols object,dfs.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. getNaN()LocalInfinity The string that represents infinity for floating-point values,i.e., dfs. getInfinity() Number syntax
| NonASCIIDigit :: | = A non-ASCII character c for whichCharacter.isDigit(c) returns true | ||||
| Non0Digit :: | = [1-Rmax] |NonASCIIDigit | ||||
| Digit :: | = [0-Rmax] |NonASCIIDigit | ||||
| GroupedNumeral :: |
| ||||
| Numeral :: | = ( (Digit+ ) |GroupedNumeral ) | ||||
| Integer :: | = ( [-+]? (Numeral ) ) | ||||
| |LocalPositivePrefixNumeralLocalPositiveSuffix | |||||
| |LocalNegativePrefixNumeralLocalNegativeSuffix | |||||
| DecimalNumeral :: | =Numeral | ||||
| |NumeralLocalDecimalSeparatorDigit* | |||||
| |LocalDecimalSeparatorDigit+ | |||||
| Exponent :: | = ( [eE] [+-]?Digit+ ) | ||||
| Decimal :: | = ( [-+]?DecimalNumeralExponent? ) | ||||
| |LocalPositivePrefixDecimalNumeralLocalPositiveSuffixExponent? | |||||
| |LocalNegativePrefixDecimalNumeralLocalNegativeSuffixExponent? | |||||
| HexFloat :: | = [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)? | ||||
| NonNumber :: | = NaN |LocalNan | Infinity |LocalInfinity | ||||
| SignedNonNumber :: | = ( [-+]?NonNumber ) | ||||
| |LocalPositivePrefixNonNumberLocalPositiveSuffix | |||||
| |LocalNegativePrefixNonNumberLocalNegativeSuffix | |||||
| Float :: | =Decimal | ||||
| |HexFloat | |||||
| |SignedNonNumber |
Whitespace is not significant in the above regular expressions.
| Constructor and Description |
|---|
Scanner(File source)Constructs a new Scanner that produces values scanned from the specified file. |
Scanner(File source,String charsetName)Constructs a new Scanner that produces values scanned from the specified file. |
Scanner(InputStream source)Constructs a new Scanner that produces values scanned from the specified input stream. |
Scanner(InputStream source,String charsetName)Constructs a new Scanner that produces values scanned from the specified input stream. |
Scanner(Path source)Constructs a new Scanner that produces values scanned from the specified file. |
Scanner(Path source,String charsetName)Constructs a new Scanner that produces values scanned from the specified file. |
Scanner(Readable source)Constructs a new Scanner that produces values scanned from the specified source. |
Scanner(ReadableByteChannel source)Constructs a new Scanner that produces values scanned from the specified channel. |
Scanner(ReadableByteChannel source,String charsetName)Constructs a new Scanner that produces values scanned from the specified channel. |
Scanner(String source)Constructs a new Scanner that produces values scanned from the specified string. |
| Modifier and Type | Method and Description |
|---|---|
void | close()Closes this scanner. |
Pattern | delimiter()Returns the Pattern thisScanner 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 thenextBigDecimal() 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 thenextBigInteger() 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 thenextBigInteger() 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 thisScanner's underlyingReadable. |
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 token if it 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 abyte. |
byte | nextByte(int radix)Scans the next token of the input as abyte. |
double | nextDouble()Scans the next token of the input as adouble. |
float | nextFloat()Scans the next token of the input as afloat. |
int | nextInt()Scans the next token of the input as anint. |
int | nextInt(int radix)Scans the next token of the input as anint. |
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 along. |
long | nextLong(int radix)Scans the next token of the input as along. |
short | nextShort()Scans the next token of the input as ashort. |
short | nextShort(int radix)Scans the next token of the input as ashort. |
int | radix()Returns this scanner's default radix. |
void | remove()The remove operation is not supported by this implementation of Iterator. |
Scanner | reset()Resets this scanner. |
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. |
public Scanner(Readable source)
Scanner that produces values scanned from the specified source.source - A character source implementing theReadable interfacepublic Scanner(InputStream source)
Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform'sdefault charset.source - An input stream to be scannedpublic Scanner(InputStream source,String charsetName)
Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the specified charset.source - An input stream to be scannedcharsetName - The encoding type used to convert bytes from the stream into characters to be scannedIllegalArgumentException - if the specified character set does not existpublic Scanner(File source) throwsFileNotFoundException
Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform'sdefault charset.source - A file to be scannedFileNotFoundException - if source is not foundpublic Scanner(File source,String charsetName) throwsFileNotFoundException
Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the specified charset.source - A file to be scannedcharsetName - The encoding type used to convert bytes from the file into characters to be scannedFileNotFoundException - if source is not foundIllegalArgumentException - if the specified encoding is not foundpublic Scanner(Path source) throwsIOException
Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform'sdefault charset.source - the path to the file to be scannedIOException - if an I/O error occurs opening sourcepublic Scanner(Path source,String charsetName) throwsIOException
Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the specified charset.source - the path to the file to be scannedcharsetName - The encoding type used to convert bytes from the file into characters to be scannedIOException - if an I/O error occurs opening sourceIllegalArgumentException - if the specified encoding is not foundpublic Scanner(String source)
Scanner that produces values scanned from the specified string.source - A string to scanpublic Scanner(ReadableByteChannel source)
Scanner that produces values scanned from the specified channel. Bytes from the source are converted into characters using the underlying platform'sdefault charset.source - A channel to scanpublic Scanner(ReadableByteChannel source,String charsetName)
Scanner that produces values scanned from the specified channel. Bytes from the source are converted into characters using the specified charset.source - A channel to scancharsetName - The encoding type used to convert bytes from the channel into characters to be scannedIllegalArgumentException - if the specified character set does not existpublic void close()
If this scanner has not yet been closed then if its underlyingreadable also implements theCloseable interface then the readable'sclose 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 anIllegalStateException.
close in interface Closeableclose in interface AutoCloseablepublic IOException ioException()
IOException last thrown by thisScanner's underlyingReadable. This method returnsnull if no such exception exists.public Pattern delimiter()
Pattern thisScanner is currently using to match delimiters.public Scanner useDelimiter(Pattern pattern)
pattern - A delimiting patternpublic Scanner useDelimiter(String pattern)
String.An invocation of this method of the formuseDelimiter(pattern) behaves in exactly the same way as the invocationuseDelimiter(Pattern.compile(pattern)).
Invoking thereset() method will set the scanner's delimiter to thedefault.
pattern - A string specifying a delimiting patternpublic Locale locale()
A scanner's locale affects many elements of its default primitive matching regular expressions; seelocalized numbers above.
public Scanner useLocale(Locale locale)
A scanner's locale affects many elements of its default primitive matching regular expressions; seelocalized numbers above.
Invoking thereset() method will set the scanner's locale to theinitial locale.
locale - A string specifying the locale to usepublic int radix()
A scanner's radix affects elements of its default number matching regular expressions; seelocalized numbers above.
public Scanner useRadix(int radix)
A scanner's radix affects elements of its default number matching regular expressions; seelocalized numbers above.
If the radix is less thanCharacter.MIN_RADIX or greater thanCharacter.MAX_RADIX, then anIllegalArgumentException is thrown.
Invoking thereset() method will set the scanner's radix to10.
radix - The radix to use when scanning numbersIllegalArgumentException - if radix is out of rangepublic MatchResult match()
IllegalStateException if no match has been performed, or if the last match was not successful.The variousnextmethods ofScanner make a match result available if they complete without throwing an exception. For instance, after an invocation of thenextInt() method that returned an int, this method returns aMatchResult for the search of theInteger regular expression defined above. Similarly thefindInLine(java.lang.String),findWithinHorizon(java.lang.String, int), andskip(java.util.regex.Pattern) methods will make a match available if they succeed.
IllegalStateException - If no match result is availablepublic String toString()
Returns the string representation of thisScanner. The string representation of aScanner contains information that may be useful for debugging. The exact format is unspecified.
public boolean hasNext()
hasNext in interface Iterator<String>IllegalStateException - if this scanner is closedIteratorpublic String next()
hasNext() returnedtrue.next in interface Iterator<String>NoSuchElementException - if no more tokens are availableIllegalStateException - if this scanner is closedIteratorpublic void remove()
Iterator.remove in interface Iterator<String>UnsupportedOperationException - if this method is invoked.Iteratorpublic boolean hasNext(String pattern)
An invocation of this method of the formhasNext(pattern) behaves in exactly the same way as the invocationhasNext(Pattern.compile(pattern)).
pattern - a string specifying the pattern to scanIllegalStateException - if this scanner is closedpublic String next(String pattern)
An invocation of this method of the formnext(pattern) behaves in exactly the same way as the invocationnext(Pattern.compile(pattern)).
pattern - a string specifying the pattern to scanNoSuchElementException - if no such tokens are availableIllegalStateException - if this scanner is closedpublic boolean hasNext(Pattern pattern)
pattern - the pattern to scan forIllegalStateException - if this scanner is closedpublic String next(Pattern pattern)
hasNext(Pattern) returnedtrue. If the match is successful, the scanner advances past the input that matched the pattern.pattern - the pattern to scan forNoSuchElementException - if no more tokens are availableIllegalStateException - if this scanner is closedpublic boolean hasNextLine()
IllegalStateException - if this scanner is closedpublic String nextLine()
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.
NoSuchElementException - if no line was foundIllegalStateException - if this scanner is closedpublic String findInLine(String pattern)
An invocation of this method of the formfindInLine(pattern) behaves in exactly the same way as the invocationfindInLine(Pattern.compile(pattern)).
pattern - a string specifying the pattern to search forIllegalStateException - if this scanner is closedpublic String findInLine(Pattern pattern)
null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the 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.
pattern - the pattern to scan forIllegalStateException - if this scanner is closedpublic String findWithinHorizon(String pattern, int horizon)
An invocation of this method of the formfindWithinHorizon(pattern) behaves in exactly the same way as the invocationfindWithinHorizon(Pattern.compile(pattern, horizon)).
pattern - a string specifying the pattern to search forIllegalStateException - if this scanner is closedIllegalArgumentException - if horizon is negativepublic String findWithinHorizon(Pattern pattern, int 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 thanhorizon 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 scanner treats the horizon as a transparent, non-anchoring bound (seeMatcher.useTransparentBounds(boolean) andMatcher.useAnchoringBounds(boolean)).
If horizon is0, 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.
pattern - the pattern to scan forIllegalStateException - if this scanner is closedIllegalArgumentException - if horizon is negativepublic Scanner skip(Pattern pattern)
If a match to the specified pattern is not found at the current position, then no input is skipped and aNoSuchElementException 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 aNoSuchElementException by using a pattern that can match nothing, e.g.,sc.skip("[ \t]*").
pattern - a string specifying the pattern to skip overNoSuchElementException - if the specified pattern is not foundIllegalStateException - if this scanner is closedpublic Scanner skip(String pattern)
An invocation of this method of the formskip(pattern) behaves in exactly the same way as the invocationskip(Pattern.compile(pattern)).
pattern - a string specifying the pattern to skip overIllegalStateException - if this scanner is closedpublic boolean hasNextBoolean()
IllegalStateException - if this scanner is closedpublic boolean nextBoolean()
InputMismatchException if the next token cannot be translated into a valid boolean value. If the match is successful, the scanner advances past the input that matched.InputMismatchException - if the next token is not a valid booleanNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextByte()
nextByte() method. The scanner does not advance past any input.IllegalStateException - if this scanner is closedpublic boolean hasNextByte(int radix)
nextByte() method. The scanner does not advance past any input.radix - the radix used to interpret the token as a byte valueIllegalStateException - if this scanner is closedpublic byte nextByte()
An invocation of this method of the formnextByte() behaves in exactly the same way as the invocationnextByte(radix), whereradix is the default radix of this scanner.
InputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic byte nextByte(int radix)
InputMismatchException if the next token cannot be translated into a valid byte value as described below. If the translation is successful, the scanner advances past the input that matched. If the next token matches theInteger regular expression defined above then the token is converted into abyte value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits viaCharacter.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string toByte.parseByte with the specified radix.
radix - the radix used to interpret the token as a byte valueInputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextShort()
nextShort() method. The scanner does not advance past any input.IllegalStateException - if this scanner is closedpublic boolean hasNextShort(int radix)
nextShort() method. The scanner does not advance past any input.radix - the radix used to interpret the token as a short valueIllegalStateException - if this scanner is closedpublic short nextShort()
An invocation of this method of the formnextShort() behaves in exactly the same way as the invocationnextShort(radix), whereradix is the default radix of this scanner.
InputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic short nextShort(int radix)
InputMismatchException if the next token cannot be translated into a valid short value as described below. If the translation is successful, the scanner advances past the input that matched. If the next token matches theInteger regular expression defined above then the token is converted into ashort value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits viaCharacter.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string toShort.parseShort with the specified radix.
radix - the radix used to interpret the token as a short valueInputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextInt()
nextInt() method. The scanner does not advance past any input.IllegalStateException - if this scanner is closedpublic boolean hasNextInt(int radix)
nextInt() method. The scanner does not advance past any input.radix - the radix used to interpret the token as an int valueIllegalStateException - if this scanner is closedpublic int nextInt()
An invocation of this method of the formnextInt() behaves in exactly the same way as the invocationnextInt(radix), whereradix is the default radix of this scanner.
InputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic int nextInt(int radix)
InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched. If the next token matches theInteger regular expression defined above then the token is converted into anint value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits viaCharacter.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string toInteger.parseInt with the specified radix.
radix - the radix used to interpret the token as an int valueInputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextLong()
nextLong() method. The scanner does not advance past any input.IllegalStateException - if this scanner is closedpublic boolean hasNextLong(int radix)
nextLong() method. The scanner does not advance past any input.radix - the radix used to interpret the token as a long valueIllegalStateException - if this scanner is closedpublic long nextLong()
An invocation of this method of the formnextLong() behaves in exactly the same way as the invocationnextLong(radix), whereradix is the default radix of this scanner.
InputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic long nextLong(int radix)
InputMismatchException if the next token cannot be translated into a valid long value as described below. If the translation is successful, the scanner advances past the input that matched. If the next token matches theInteger regular expression defined above then the token is converted into along value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits viaCharacter.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string toLong.parseLong with the specified radix.
radix - the radix used to interpret the token as an int valueInputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextFloat()
nextFloat() method. The scanner does not advance past any input.IllegalStateException - if this scanner is closedpublic float nextFloat()
InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that matched. If the next token matches theFloat regular expression defined above then the token is converted into afloat value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits viaCharacter.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string toFloat.parseFloat. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed toFloat.parseFloat as appropriate.
InputMismatchException - if the next token does not match theFloat regular expression, or is out of rangeNoSuchElementException - if input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextDouble()
nextDouble() method. The scanner does not advance past any input.IllegalStateException - if this scanner is closedpublic double nextDouble()
InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched. If the next token matches theFloat regular expression defined above then the token is converted into adouble value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits viaCharacter.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string toDouble.parseDouble. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed toDouble.parseDouble as appropriate.
InputMismatchException - if the next token does not match theFloat regular expression, or is out of rangeNoSuchElementException - if the input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextBigInteger()
BigInteger in the default radix using thenextBigInteger() method. The scanner does not advance past any input.BigIntegerIllegalStateException - if this scanner is closedpublic boolean hasNextBigInteger(int radix)
BigInteger in the specified radix using thenextBigInteger() method. The scanner does not advance past any input.radix - the radix used to interpret the token as an integerBigIntegerIllegalStateException - if this scanner is closedpublic BigInteger nextBigInteger()
BigInteger. An invocation of this method of the formnextBigInteger() behaves in exactly the same way as the invocationnextBigInteger(radix), whereradix is the default radix of this scanner.
InputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if the input is exhaustedIllegalStateException - if this scanner is closedpublic BigInteger nextBigInteger(int radix)
BigInteger. If the next token matches theInteger regular expression defined above then the token is converted into aBigInteger value as if by removing all group separators, mapping non-ASCII digits into ASCII digits via theCharacter.digit, and passing the resulting string to theBigInteger(String, int) constructor with the specified radix.
radix - the radix used to interpret the tokenInputMismatchException - if the next token does not match theInteger regular expression, or is out of rangeNoSuchElementException - if the input is exhaustedIllegalStateException - if this scanner is closedpublic boolean hasNextBigDecimal()
BigDecimal using thenextBigDecimal() method. The scanner does not advance past any input.BigDecimalIllegalStateException - if this scanner is closedpublic BigDecimal nextBigDecimal()
BigDecimal. If the next token matches theDecimal regular expression defined above then the token is converted into aBigDecimal value as if by removing all group separators, mapping non-ASCII digits into ASCII digits via theCharacter.digit, and passing the resulting string to theBigDecimal(String) constructor.
InputMismatchException - if the next token does not match theDecimal regular expression, or is out of rangeNoSuchElementException - if the input is exhaustedIllegalStateException - if this scanner is closedpublic Scanner reset()
Resetting a scanner discards all of its explicit state information which may have been changed by invocations ofuseDelimiter(java.util.regex.Pattern),useLocale(java.util.Locale), oruseRadix(int).
An invocation of this method of the formscanner.reset() behaves in exactly the same way as the invocation
scanner.useDelimiter("\\p{javaWhitespace}+") .useLocale(Locale.getDefault()) .useRadix(10);