|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectScanner
public final class Scanner
A simple text scanner which can parse primitive types and strings using regular expressions.
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.
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:
Recent changes:
% java -Dscanner.echo=1 MyProgramThatUsesScanner < input.txt
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
void |
close()
Closes this scanner. |
static java.lang.String |
getStackTraceAsString(java.lang.Throwable e)
|
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. |
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. |
static boolean |
inHasMethod()
|
java.io.IOException |
ioException()
Returns the IOException last thrown by this Scanner. |
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. |
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. |
int |
radix()
Returns this scanner's default radix. |
void |
remove()
The remove operation is not supported by this implementation of Iterator. |
java.lang.String |
toString()
Returns the string representation of this Scanner. |
Scanner |
useRadix(int radix)
Sets this scanner's default radix to the specified radix. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Scanner(java.io.File source) throws java.io.FileNotFoundException
source
- A file to be scanned
java.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 scanMethod Detail |
---|
public void close()
If this scanner is already closed then invoking this method will have no effect.
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 integer
java.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 value
java.lang.IllegalStateException
- if this scanner is closedpublic 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 value
java.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 value
java.lang.IllegalStateException
- if this scanner is closedpublic java.io.IOException ioException()
public static java.lang.String getStackTraceAsString(java.lang.Throwable e)
public static boolean inHasMethod()
public java.lang.String next()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic java.math.BigDecimal nextBigDecimal()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic java.math.BigInteger nextBigInteger()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic java.math.BigInteger nextBigInteger(int radix)
radix
- the radix used to interpret the token
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic boolean nextBoolean()
java.util.NoSuchElementException
- if the input is exhausted
java.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 exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic byte nextByte(int radix)
radix
- the radix used to interpret the token as a byte value
java.util.InputMismatchException
- if the next token does not match the
Integer regular expression, or is out of range
java.util.NoSuchElementException
- if input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic double nextDouble()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic float nextFloat()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic int nextInt()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic int nextInt(int radix)
radix
- the radix used to interpret the token as an int value
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic java.lang.String nextLine()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic long nextLong()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic long nextLong(int radix)
radix
- the radix used to interpret the token as a long value
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic short nextShort()
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic short nextShort(int radix)
radix
- the radix used to interpret the token as a short value
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic int radix()
A scanner's radix affects elements of its default number matching regular expressions.
java.util.NoSuchElementException
- if the input is exhausted
java.lang.IllegalStateException
- if this scanner is closedpublic void remove()
java.lang.UnsupportedOperationException
- if this method is invoked.public java.lang.String toString()
toString
in class java.lang.Object
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
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |