public final class Scanner
extends java.lang.Object
This class was written by and for students and professors at University of Washington. It is used here as a holdover until sdk 1.5 is released and installed.
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 RC1, 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.
This implementation does not include the static factory Scanner.create methods as included in J2SE 1.5.0 beta 1.
Some notable differences from java.util.Scanner are the following:
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
DELIMITER |
private static int |
EOF |
private boolean |
m_closed |
private java.io.IOException |
m_ioException |
private java.io.LineNumberReader |
m_lnReader |
private java.lang.StringBuilder |
m_previousNextBuffer |
private int |
m_radix |
private java.io.PushbackReader |
m_reader |
private static int |
PUSHBACK_BUFFER_SIZE |
Constructor and Description |
---|
Scanner(java.io.File source)
Constructs a new Scanner that produces values scanned from the specified file.
|
Scanner(java.io.InputStream source)
Constructs a new Scanner that produces values scanned from the specified input stream.
|
Scanner(java.io.Reader source)
Constructs a new Scanner that produces values scanned from the specified source.
|
Scanner(java.lang.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.
|
private java.util.NoSuchElementException |
getNoSuchElementException() |
boolean |
hasNext()
Returns true if this scanner has another token in its input.
|
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.
|
private boolean |
hasNextChar() |
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.
|
java.io.IOException |
ioException()
Returns the IOException last thrown by this Scanner.
|
private boolean |
isWhitespace(int chr) |
java.lang.String |
next()
Finds and returns the next complete token from this scanner.
|
java.math.BigDecimal |
nextBigDecimal()
Scans the next token of the input as a BigDecimal.
|
java.math.BigInteger |
nextBigInteger()
Scans the next token of the input as a BigInteger.
|
java.math.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.
|
private int |
nextChar() |
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.
|
java.lang.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.
|
private java.lang.String |
nextToken() |
private int |
peek() |
int |
radix()
Returns this scanner's default radix.
|
void |
remove()
The remove operation is not supported by this implementation of Iterator.
|
private void |
setIoException(java.io.IOException ioe) |
java.lang.String |
toString()
Returns the string representation of this Scanner.
|
private void |
undoNext() |
private void |
unread(int chr) |
Scanner |
useRadix(int radix)
Sets this scanner's default radix to the specified radix.
|
private static final java.lang.String DELIMITER
private static final int EOF
private static final int PUSHBACK_BUFFER_SIZE
private boolean m_closed
private java.io.IOException m_ioException
private final java.io.LineNumberReader m_lnReader
private final java.lang.StringBuilder m_previousNextBuffer
private int m_radix
private final java.io.PushbackReader m_reader
public Scanner(java.io.File source) throws java.io.FileNotFoundException
source
- A file to be scannedjava.io.FileNotFoundException
- if source is not foundpublic Scanner(java.io.InputStream source)
source
- An input stream to be scannedpublic Scanner(java.io.Reader source)
source
- A character source to scanpublic Scanner(java.lang.String source)
source
- A string to scanpublic void close()
If this scanner is already closed then invoking this method will have no effect.
private java.util.NoSuchElementException getNoSuchElementException()
public boolean hasNext()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextBigDecimal()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextBigInteger()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextBigInteger(int radix)
radix
- the radix used to interpret the token as an integerjava.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextBoolean()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextByte()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextByte(int radix)
radix
- the radix used to interpret the token as a byte valuejava.lang.IllegalStateException
- if this scanner is closedprivate boolean hasNextChar()
public boolean hasNextDouble()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextFloat()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextInt()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextInt(int radix)
radix
- the radix used to interpret the token as an int valuejava.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextLine()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextLong()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextLong(int radix)
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextShort()
java.lang.IllegalStateException
- if this scanner is closedpublic boolean hasNextShort(int radix)
radix
- the radix used to interpret the token as a short valuejava.lang.IllegalStateException
- if this scanner is closedpublic java.io.IOException ioException()
private boolean isWhitespace(int chr)
public java.lang.String next()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic java.math.BigDecimal nextBigDecimal()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic java.math.BigInteger nextBigInteger()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic java.math.BigInteger nextBigInteger(int radix)
radix
- the radix used to interpret the tokenjava.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic boolean nextBoolean()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- 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.
java.util.NoSuchElementException
- if input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic byte nextByte(int radix)
radix
- the radix used to interpret the token as a byte valuejava.util.InputMismatchException
- if the next token does not match the Integer regular expression, or is out of rangejava.util.NoSuchElementException
- if input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedprivate int nextChar()
public double nextDouble()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic float nextFloat()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic int nextInt()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic int nextInt(int radix)
radix
- the radix used to interpret the token as an int valuejava.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic java.lang.String nextLine()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic long nextLong()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic long nextLong(int radix)
radix
- the radix used to interpret the token as a long valuejava.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic short nextShort()
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic short nextShort(int radix)
radix
- the radix used to interpret the token as a short valuejava.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedprivate java.lang.String nextToken()
private int peek() throws java.io.IOException
java.io.IOException
public int radix()
A scanner's radix affects elements of its default number matching regular expressions.
java.util.NoSuchElementException
- if the input is exhaustedjava.lang.IllegalStateException
- if this scanner is closedpublic void remove()
java.lang.UnsupportedOperationException
- if this method is invoked.private void setIoException(java.io.IOException ioe)
public java.lang.String toString()
toString
in class java.lang.Object
private void undoNext()
private void unread(int chr) throws java.io.IOException
java.io.IOException
public Scanner 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.
java.lang.IllegalArgumentException
- if radix is out of range