Class Scanner

java.lang.Object
  extended by Scanner

public final class Scanner
extends java.lang.Object

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:


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

Scanner

public Scanner(java.io.File source)
        throws java.io.FileNotFoundException
Constructs a new Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset.

Parameters:
source - A file to be scanned
Throws:
java.io.FileNotFoundException - if source is not found

Scanner

public Scanner(java.io.InputStream source)
Constructs a new Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform's default charset.

Parameters:
source - An input stream to be scanned

Scanner

public Scanner(java.io.Reader source)
Constructs a new Scanner that produces values scanned from the specified source.

Parameters:
source - A character source to scan

Scanner

public Scanner(java.lang.String source)
Constructs a new Scanner that produces values scanned from the specified string.

Parameters:
source - A string to scan
Method Detail

close

public void close()
Closes this scanner.

If this scanner is already closed then invoking this method will have no effect.


hasNext

public boolean hasNext()
Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

Returns:
true if and only if this scanner has another token
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextBigDecimal

public boolean hasNextBigDecimal()
Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid BigDecimal
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextBigInteger

public 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. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid BigInteger
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextBigInteger

public 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. The scanner does not advance past any input.

Parameters:
radix - the radix used to interpret the token as an integer
Returns:
true if and only if this scanner's next token is a valid BigInteger
Throws:
java.lang.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. 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:
java.lang.IllegalStateException - if this scanner is closed

hasNextByte

public 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. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid byte value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextByte

public 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. The scanner does not advance past any input.

Parameters:
radix - the radix used to interpret the token as a byte value
Returns:
true if and only if this scanner's next token is a valid byte value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextDouble

public boolean hasNextDouble()
Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid double value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextFloat

public boolean hasNextFloat()
Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid float value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextInt

public 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. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid int value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextInt

public 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. The scanner does not advance past any input.

Parameters:
radix - the radix used to interpret the token as an int value
Returns:
true if and only if this scanner's next token is a valid int value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextLine

public boolean hasNextLine()
Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

Returns:
true if and only if this scanner has another line of input
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextLong

public 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. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid long value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextLong

public 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. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid long value
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextShort

public 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. The scanner does not advance past any input.

Returns:
true if and only if this scanner's next token is a valid short value in the default radix
Throws:
java.lang.IllegalStateException - if this scanner is closed

hasNextShort

public 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. The scanner does not advance past any input.

Parameters:
radix - the radix used to interpret the token as a short value
Returns:
true if and only if this scanner's next token is a valid short value in the specified radix
Throws:
java.lang.IllegalStateException - if this scanner is closed

ioException

public java.io.IOException ioException()
Returns the IOException last thrown by this Scanner. This method returns null if no such exception exists.

Returns:
the last exception thrown by this scanner's readable

getStackTraceAsString

public static java.lang.String getStackTraceAsString(java.lang.Throwable e)

inHasMethod

public static boolean inHasMethod()

next

public java.lang.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.

Returns:
the next token
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextBigDecimal

public java.math.BigDecimal nextBigDecimal()
Scans the next token of the input as a BigDecimal.

Returns:
the BigDecimal scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextBigInteger

public java.math.BigInteger nextBigInteger()
Scans the next token of the input as a BigInteger.

Returns:
the BigInteger scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextBigInteger

public java.math.BigInteger nextBigInteger(int radix)
Scans the next token of the input as a BigInteger.

Parameters:
radix - the radix used to interpret the token
Returns:
the BigInteger scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.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 FormatException 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:
java.util.NoSuchElementException - if the input is exhausted
java.lang.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:
java.util.NoSuchElementException - if input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextByte

public byte nextByte(int radix)
Scans the next token of the input as a byte.

Parameters:
radix - the radix used to interpret the token as a byte value
Returns:
the byte scanned from the input
Throws:
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 closed

nextDouble

public double nextDouble()
Scans the next token of the input as a double. This method will throw NumberFormatException 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.

Returns:
the double scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextFloat

public float nextFloat()
Scans the next token of the input as a float. This method will throw NumberFormatException if the next token cannot be translated into a valid float value. If the translation is successful, the scanner advances past the input that matched.

Returns:
the float scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextInt

public int nextInt()
Scans the next token of the input as an int. This method will throw NumberFormatException if the next token cannot be translated into a valid int value. If the translation is successful, the scanner advances past the input that matched.

Returns:
the int scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextInt

public int nextInt(int radix)
Scans the next token of the input as an int. This method will throw FormatException if the next token cannot be translated into a valid int value. If the translation is successful, the scanner advances past the input that matched.

Parameters:
radix - the radix used to interpret the token as an int value
Returns:
the int scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextLine

public java.lang.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:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextLong

public long nextLong()
Scans the next token of the input as a long. This method will throw NumberFormatException if the next token cannot be translated into a valid long value. If the translation is successful, the scanner advances past the input that matched.

Returns:
the long scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextLong

public long nextLong(int radix)
Scans the next token of the input as a long. This method will throw FormatException if the next token cannot be translated into a valid long value. If the translation is successful, the scanner advances past the input that matched.

Parameters:
radix - the radix used to interpret the token as a long value
Returns:
the long scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextShort

public short nextShort()
Scans the next token of the input as a short. This method will throw NumberFormatException if the next token cannot be translated into a valid short value. If the translation is successful, the scanner advances past the input that matched.

Returns:
the short scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

nextShort

public short nextShort(int radix)
Scans the next token of the input as a short. This method will throw FormatException if the next token cannot be translated into a valid short value. If the translation is successful, the scanner advances past the input that matched.

Parameters:
radix - the radix used to interpret the token as a short value
Returns:
the long scanned from the input
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

radix

public int radix()
Returns this scanner's default radix.

A scanner's radix affects elements of its default number matching regular expressions.

Returns:
the default radix of this scanner
Throws:
java.util.NoSuchElementException - if the input is exhausted
java.lang.IllegalStateException - if this scanner is closed

remove

public void remove()
The remove operation is not supported by this implementation of Iterator.

Throws:
java.lang.UnsupportedOperationException - if this method is invoked.

toString

public java.lang.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 java.lang.Object
Returns:
The string representation of this scanner

useRadix

public Scanner useRadix(int radix)
Sets this scanner's default radix to the specified 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.

Throws:
java.lang.IllegalArgumentException - if radix is out of range