public final class SimpleScanner extends Object
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
This class is based on a subset of the functionality of Sun's java.util.Scanner class from J2SE v1.5.0, with some code borrowed from the TextReader class written by Stuart Reges of the University of Washington. It should work with 'lazy input' from the keyboard as needed.
Some notable differences from java.util.Scanner are the following:
Recent changes:
% java -Dscanner.reuse=1 MyProgramThatUsesScanner
% java -Dscanner.echo=1 MyProgramThatUsesScanner < input.txt
Modifier and Type | Field and Description |
---|---|
static String |
SCANNER_APPLET_MODE_PROPERTY_NAME |
static String |
SCANNER_ECHO_PROPERTY_NAME |
static String |
SCANNER_REUSE_PROPERTY_NAME |
static String |
SCANNER_WORKING_DIR_PROPERTY_NAME |
Constructor and Description |
---|
SimpleScanner(File source)
Constructs a new Scanner that produces values scanned from the specified
file.
|
SimpleScanner(InputStream source)
Constructs a new Scanner that produces values scanned from the specified
input stream.
|
SimpleScanner(Reader source)
Constructs a new Scanner that produces values scanned from the specified
source.
|
SimpleScanner(String source)
Constructs a new Scanner that produces values scanned from the specified
string.
|
Modifier and Type | Method and Description |
---|---|
static void |
clearCache() |
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.
|
static boolean |
getAppletMode() |
static boolean |
getEcho() |
static boolean |
getReuse() |
static String |
getWorkingDir() |
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 the next token if it 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.
|
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.
|
Locale |
locale()
Returns this scanner's locale.
|
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.
|
static void |
setAppletMode(boolean value) |
static void |
setEcho(boolean value) |
void |
setEchoForce(boolean value) |
void |
setEchoStream(PrintStream stream) |
static void |
setReuse(boolean value) |
static void |
setWorkingDir(String value) |
SimpleScanner |
skip(Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
|
SimpleScanner |
skip(String pattern)
Skips input that matches a pattern constructed from the specified string.
|
String |
toString()
Returns the string representation of this Scanner.
|
SimpleScanner |
useDelimiter(Pattern pattern)
Sets this scanner's delimiting pattern to the specified pattern.
|
SimpleScanner |
useDelimiter(String pattern)
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
|
SimpleScanner |
useLocale(Locale locale)
Sets this scanner's locale to the specified locale.
|
SimpleScanner |
useRadix(int radix)
Sets this scanner's default radix to the specified radix.
|
public static final String SCANNER_APPLET_MODE_PROPERTY_NAME
public static final String SCANNER_ECHO_PROPERTY_NAME
public static final String SCANNER_REUSE_PROPERTY_NAME
public static final String SCANNER_WORKING_DIR_PROPERTY_NAME
public SimpleScanner(File source)
source
- A file to be scannedIORuntimeException
- if source is not found or cannot be readpublic SimpleScanner(InputStream source)
source
- An input stream to be scannedpublic SimpleScanner(Reader source)
source
- A character source to scanpublic SimpleScanner(String source)
source
- A string to scanpublic static void clearCache()
public static boolean getEcho()
public static boolean getAppletMode()
public static boolean getReuse()
public static String getWorkingDir()
public static void setAppletMode(boolean value)
public static void setEcho(boolean value)
public static void setReuse(boolean value)
public static void setWorkingDir(String value)
public void close()
If this scanner is already closed then invoking this method will have no effect.
public Pattern delimiter()
public String findWithinHorizon(Pattern pattern, int horizon)
pattern
- the pattern to scan forIllegalStateException
- if this scanner is closedIllegalArgumentException
- if horizon is negativepublic String findWithinHorizon(String pattern, int horizon)
pattern
- a string specifying the pattern to search forIllegalStateException
- if this scanner is closedIllegalArgumentException
- if horizon is negativepublic String findInLine(Pattern pattern)
pattern
- the pattern to scan forIllegalStateException
- - if this scanner is closedpublic String findInLine(String pattern)
pattern
- a string specifying the pattern to search forIllegalStateException
- if this scanner is closedpublic boolean hasNext()
IllegalStateException
- if this scanner is closedpublic boolean hasNext(Pattern pattern)
pattern
- the pattern to scan forIllegalStateException
- if this scanner is closedpublic boolean hasNext(String pattern)
pattern
- a string specifying the pattern to scanNoSuchElementException
- if no such tokens are availableIllegalStateException
- if this scanner is closedpublic boolean hasNextBigDecimal()
IllegalStateException
- if this scanner is closedpublic boolean hasNextBigInteger()
IllegalStateException
- if this scanner is closedpublic boolean hasNextBigInteger(int radix)
radix
- the radix used to interpret the token as an integerIllegalStateException
- if this scanner is closedpublic boolean hasNextBoolean()
IllegalStateException
- if this scanner is closedpublic boolean hasNextByte()
IllegalStateException
- if this scanner is closedpublic boolean hasNextByte(int radix)
radix
- the radix used to interpret the token as a byte valueIllegalStateException
- if this scanner is closedpublic boolean hasNextDouble()
IllegalStateException
- if this scanner is closedpublic boolean hasNextFloat()
IllegalStateException
- if this scanner is closedpublic boolean hasNextInt()
IllegalStateException
- if this scanner is closedpublic boolean hasNextInt(int radix)
radix
- the radix used to interpret the token as an int valueIllegalStateException
- if this scanner is closedpublic boolean hasNextLine()
IllegalStateException
- if this scanner is closedpublic boolean hasNextLong()
IllegalStateException
- if this scanner is closedpublic boolean hasNextLong(int radix)
IllegalStateException
- if this scanner is closedpublic boolean hasNextShort()
IllegalStateException
- if this scanner is closedpublic boolean hasNextShort(int radix)
radix
- the radix used to interpret the token as a short valueIllegalStateException
- if this scanner is closedpublic IOException ioException()
public Locale locale()
public String next()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic String next(Pattern pattern)
pattern
- the pattern to scan forNoSuchElementException
- if no more tokens are availableIllegalStateException
- if this scanner is closedpublic String next(String pattern)
pattern
- a string specifying the pattern to scanNoSuchElementException
- if no such tokens are availableIllegalStateException
- if this scanner is closedpublic BigDecimal nextBigDecimal()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic BigInteger nextBigInteger()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic BigInteger nextBigInteger(int radix)
radix
- the radix used to interpret the tokenNoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic boolean nextBoolean()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic byte nextByte()
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.
NoSuchElementException
- if input is exhaustedIllegalStateException
- if this scanner is closedpublic byte nextByte(int radix)
radix
- the radix used to interpret the token as a byte valueInputMismatchException
- if the next token does not match the
Integer regular expression, or is out of rangeNoSuchElementException
- if input is exhaustedIllegalStateException
- if this scanner is closedpublic double nextDouble()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic float nextFloat()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic int nextInt()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic int nextInt(int radix)
radix
- the radix used to interpret the token as an int valueNoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic String nextLine()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic long nextLong()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic long nextLong(int radix)
radix
- the radix used to interpret the token as a long valueNoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic short nextShort()
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic short nextShort(int radix)
radix
- the radix used to interpret the token as a short valueNoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic int radix()
A scanner's radix affects elements of its default number matching regular expressions.
NoSuchElementException
- if the input is exhaustedIllegalStateException
- if this scanner is closedpublic void remove()
UnsupportedOperationException
- if this method is invoked.public void setEchoForce(boolean value)
public void setEchoStream(PrintStream stream)
public SimpleScanner skip(Pattern pattern)
pattern
- a string specifying the pattern to skip overNoSuchElementException
- if the specified pattern is not foundIllegalStateException
- if this scanner is closedpublic SimpleScanner skip(String pattern)
pattern
- a string specifying the pattern to skip overIllegalStateException
- if this scanner is closedpublic String toString()
public SimpleScanner useDelimiter(Pattern pattern)
pattern
- A delimiting patternpublic SimpleScanner useDelimiter(String pattern)
pattern
- A string specifying a delimiting patternpublic SimpleScanner useLocale(Locale locale)
locale
- A string specifying the locale to usepublic SimpleScanner useRadix(int radix)
A scanner's radix affects elements of its default number matching regular expressions.
If the radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, then an IllegalArgumentException is thrown.
IllegalArgumentException
- if radix is out of range