GeezerVison is showing docs.oracle.com — Scanner (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util
Class Scanner
-
-
Constructor Summary
Constructors
| Constructor |
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.
|
-
Method Summary
All Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
void |
close() |
Closes this scanner.
|
Pattern |
delimiter() |
Returns the Pattern this Scanner is currently
using to match delimiters.
|
String |
findInLine(Pattern pattern) |
Attempts to find the next occurrence of the specified pattern ignoring
delimiters.
|
String |
findInLine(String pattern) |
Attempts to find the next occurrence of a pattern constructed from the
specified string, ignoring delimiters.
|
String |
findWithinHorizon(Pattern pattern,
int horizon) |
Attempts to find the next occurrence of the specified pattern.
|
String |
findWithinHorizon(String pattern,
int horizon) |
Attempts to find the next occurrence of a pattern constructed from the
specified string, ignoring delimiters.
|
boolean |
hasNext() |
Returns true if this scanner has another token in its input.
|
boolean |
hasNext(Pattern pattern) |
Returns true if the next complete token matches the specified pattern.
|
boolean |
hasNext(String pattern) |
Returns true if the next token matches the pattern constructed from the
specified string.
|
boolean |
hasNextBigDecimal() |
Returns true if the next token in this scanner's input can be
interpreted as a BigDecimal using the
nextBigDecimal() method.
|
boolean |
hasNextBigInteger() |
Returns true if the next token in this scanner's input can be
interpreted as a BigInteger in the default radix using the
nextBigInteger() method.
|
boolean |
hasNextBigInteger(int radix) |
Returns true if the next token in this scanner's input can be
interpreted as a BigInteger in the specified radix using
the nextBigInteger() method.
|
boolean |
hasNextBoolean() |
Returns true if the next token in this scanner's input can be
interpreted as a boolean value using a case insensitive pattern
created from the string "true|false".
|
boolean |
hasNextByte() |
Returns true if the next token in this scanner's input can be
interpreted as a byte value in the default radix using the
nextByte() method.
|
boolean |
hasNextByte(int radix) |
Returns true if the next token in this scanner's input can be
interpreted as a byte value in the specified radix using the
nextByte() method.
|
boolean |
hasNextDouble() |
Returns true if the next token in this scanner's input can be
interpreted as a double value using the nextDouble()
method.
|
boolean |
hasNextFloat() |
Returns true if the next token in this scanner's input can be
interpreted as a float value using the nextFloat()
method.
|
boolean |
hasNextInt() |
Returns true if the next token in this scanner's input can be
interpreted as an int value in the default radix using the
nextInt() method.
|
boolean |
hasNextInt(int radix) |
Returns true if the next token in this scanner's input can be
interpreted as an int value in the specified radix using the
nextInt() method.
|
boolean |
hasNextLine() |
Returns true if there is another line in the input of this scanner.
|
boolean |
hasNextLong() |
Returns true if the next token in this scanner's input can be
interpreted as a long value in the default radix using the
nextLong() method.
|
boolean |
hasNextLong(int radix) |
Returns true if the next token in this scanner's input can be
interpreted as a long value in the specified radix using the
nextLong() method.
|
boolean |
hasNextShort() |
Returns true if the next token in this scanner's input can be
interpreted as a short value in the default radix using the
nextShort() method.
|
boolean |
hasNextShort(int radix) |
Returns true if the next token in this scanner's input can be
interpreted as a short value in the specified radix using the
nextShort() method.
|
IOException |
ioException() |
Returns the IOException last thrown by this
Scanner's underlying Readable.
|
Locale |
locale() |
Returns this scanner's locale.
|
MatchResult |
match() |
Returns the match result of the last scanning operation performed
by this scanner.
|
String |
next() |
Finds and returns the next complete token from this scanner.
|
String |
next(Pattern pattern) |
Returns the next 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 a byte.
|
byte |
nextByte(int radix) |
Scans the next token of the input as a byte.
|
double |
nextDouble() |
Scans the next token of the input as a double.
|
float |
nextFloat() |
Scans the next token of the input as a float.
|
int |
nextInt() |
Scans the next token of the input as an int.
|
int |
nextInt(int radix) |
Scans the next token of the input as an int.
|
String |
nextLine() |
Advances this scanner past the current line and returns the input
that was skipped.
|
long |
nextLong() |
Scans the next token of the input as a long.
|
long |
nextLong(int radix) |
Scans the next token of the input as a long.
|
short |
nextShort() |
Scans the next token of the input as a short.
|
short |
nextShort(int radix) |
Scans the next token of the input as a short.
|
int |
radix() |
Returns this scanner's default radix.
|
void |
remove() |
The remove operation is not supported by this implementation of
Iterator.
|
Scanner |
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.
|
-
-
Constructor Detail
-
Scanner
public Scanner(InputStream source,
String charsetName)
Constructs a new Scanner that produces values scanned
from the specified input stream. Bytes from the stream are converted
into characters using the specified charset.
- Parameters:
source - An input stream to be scanned
charsetName - The encoding type used to convert bytes from the
stream into characters to be scanned
- Throws:
IllegalArgumentException - if the specified character set
does not exist
-
Scanner
public Scanner(File source,
String charsetName)
throws FileNotFoundException
Constructs a new Scanner that produces values scanned
from the specified file. Bytes from the file are converted into
characters using the specified charset.
- Parameters:
source - A file to be scanned
charsetName - The encoding type used to convert bytes from the file
into characters to be scanned
- Throws:
FileNotFoundException - if source is not found
IllegalArgumentException - if the specified encoding is
not found
-
Scanner
public Scanner(Path source,
String charsetName)
throws IOException
Constructs a new Scanner that produces values scanned
from the specified file. Bytes from the file are converted into
characters using the specified charset.
- Parameters:
source - the path to the file to be scanned
charsetName - The encoding type used to convert bytes from the file
into characters to be scanned
- Throws:
IOException - if an I/O error occurs opening source
IllegalArgumentException - if the specified encoding is not found
- Since:
- 1.7
-
Scanner
public Scanner(ReadableByteChannel source,
String charsetName)
Constructs a new Scanner that produces values scanned
from the specified channel. Bytes from the source are converted into
characters using the specified charset.
- Parameters:
source - A channel to scan
charsetName - The encoding type used to convert bytes from the
channel into characters to be scanned
- Throws:
IllegalArgumentException - if the specified character set
does not exist
-
Method Detail
-
close
public void close()
Closes this scanner.
If this scanner has not yet been closed then if its underlying
readable also implements the Closeable interface then the readable's close method
will be invoked. If this scanner is already closed then invoking this
method will have no effect.
Attempting to perform search operations after a scanner has
been closed will result in an IllegalStateException.
- Specified by:
close in interface Closeable
- Specified by:
close in interface AutoCloseable
-
locale
public Locale locale()
Returns this scanner's locale.
A scanner's locale affects many elements of its default
primitive matching regular expressions; see
localized numbers above.
- Returns:
- this scanner's locale
-
radix
public int radix()
Returns this scanner's default radix.
A scanner's radix affects elements of its default
number matching regular expressions; see
localized numbers above.
- Returns:
- the default radix of this scanner
-
toString
public String toString()
Returns the string representation of this Scanner. The
string representation of a Scanner contains information
that may be useful for debugging. The exact format is unspecified.
- Overrides:
toString in class Object
- Returns:
- The string representation of this scanner
-
next
public String next()
Finds and returns the next complete token from this scanner.
A complete token is preceded and followed by input that matches
the delimiter pattern. This method may block while waiting for input
to scan, even if a previous invocation of hasNext() returned
true.
- Specified by:
next in interface Iterator<String>
- Returns:
- the next token
- Throws:
NoSuchElementException - if no more tokens are available
IllegalStateException - if this scanner is closed
- See Also:
Iterator
-
hasNext
public boolean hasNext(Pattern pattern)
Returns true if the next complete token matches the specified pattern.
A complete token is prefixed and postfixed by input that matches
the delimiter pattern. This method may block while waiting for input.
The scanner does not advance past any input.
- Parameters:
pattern - the pattern to scan for
- Returns:
- true if and only if this scanner has another token matching
the specified pattern
- Throws:
IllegalStateException - if this scanner is closed
-
nextLine
public String nextLine()
Advances this scanner past the current line and returns the input
that was skipped.
This method returns the rest of the current line, excluding any line
separator at the end. The position is set to the beginning of the next
line.
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.
- Returns:
- the line that was skipped
- Throws:
NoSuchElementException - if no line was found
IllegalStateException - if this scanner is closed
-
hasNextBoolean
public 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". The scanner does not
advance past the input that matched.
- Returns:
- true if and only if this scanner's next token is a valid
boolean value
- Throws:
IllegalStateException - if this scanner is closed
-
nextBoolean
public boolean nextBoolean()
Scans the next token of the input into a boolean value and returns
that value. This method will throw 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.
- Returns:
- the boolean scanned from the input
- Throws:
InputMismatchException - if the next token is not a valid boolean
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
-
nextByte
public byte nextByte()
Scans the next token of the input as a byte.
An invocation of this method of the form
nextByte() behaves in exactly the same way as the
invocation nextByte(radix), where radix
is the default radix of this scanner.
- Returns:
- the byte scanned from the input
- Throws:
InputMismatchException - if the next token does not match the Integer
regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
-
nextShort
public short nextShort()
Scans the next token of the input as a short.
An invocation of this method of the form
nextShort() behaves in exactly the same way as the
invocation nextShort(radix), where radix
is the default radix of this scanner.
- Returns:
- the short scanned from the input
- Throws:
InputMismatchException - if the next token does not match the Integer
regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
-
nextInt
public int nextInt()
Scans the next token of the input as an int.
An invocation of this method of the form
nextInt() behaves in exactly the same way as the
invocation nextInt(radix), where radix
is the default radix of this scanner.
- Returns:
- the int scanned from the input
- Throws:
InputMismatchException - if the next token does not match the Integer
regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
-
nextLong
public long nextLong()
Scans the next token of the input as a long.
An invocation of this method of the form
nextLong() behaves in exactly the same way as the
invocation nextLong(radix), where radix
is the default radix of this scanner.
- Returns:
- the long scanned from the input
- Throws:
InputMismatchException - if the next token does not match the Integer
regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
-
nextFloat
public float nextFloat()
Scans the next token of the input as a float.
This method will throw 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 the Float regular expression defined above
then the token is converted into a float value as if by
removing all locale specific prefixes, group separators, and locale
specific suffixes, then mapping non-ASCII digits into ASCII
digits via Character.digit, prepending a
negative sign (-) if the locale specific negative prefixes and suffixes
were present, and passing the resulting string to
Float.parseFloat. If the token matches
the localized NaN or infinity strings, then either "Nan" or "Infinity"
is passed to Float.parseFloat as
appropriate.
- Returns:
- the float scanned from the input
- Throws:
InputMismatchException - if the next token does not match the Float
regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
-
reset
public Scanner reset()
Resets this scanner.
Resetting a scanner discards all of its explicit state
information which may have been changed by invocations of useDelimiter(java.util.regex.Pattern), useLocale(java.util.Locale), or useRadix(int).
An invocation of this method of the form
scanner.reset() behaves in exactly the same way as the
invocation
scanner.useDelimiter("\\p{javaWhitespace}+")
.useLocale(Locale.getDefault(Locale.Category.FORMAT))
.useRadix(10);
- Returns:
- this scanner
- Since:
- 1.6
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|