FileDocCategorySizeDatePackage
Scanner.javaAPI DocJava SE 5 API97160Fri Aug 26 14:57:24 BST 2005java.util

Scanner

public final class Scanner extends Object implements Iterator
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.

For example, this code allows a user to read a number from System.in:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

As another example, this code allows long types to be assigned from entries in a file myNumbers:

Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}

The scanner can also use delimiters other than whitespace. This example reads several items in from a string:

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close(); 

prints the following output:

1
2
red
blue 

The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i);
s.close(); 

The default whitespace delimiter used by a scanner is as recognized by {@link java.lang.Character}.{@link java.lang.Character#isWhitespace(char) isWhitespace}.

A scanning operation may block waiting for input.

The {@link #next} and {@link #hasNext} methods and their primitive-type companion methods (such as {@link #nextInt} and {@link #hasNextInt}) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.

The {@link #findInLine}, {@link #findWithinHorizon}, and {@link #skip} methods operate independently of the delimiter pattern. These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant. These methods may block waiting for more input.

When a scanner throws an {@link InputMismatchException}, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern "\\s+" will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern "\\s" could return empty tokens since it only passes one space at a time.

A scanner can read text from any object which implements the {@link java.lang.Readable} interface. If an invocation of the underlying readable's {@link java.lang.Readable#read} method throws an {@link java.io.IOException} then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the {@link #ioException} method.

When a Scanner is closed, it will close its input source if the source implements the {@link java.io.Closeable} interface.

A Scanner is not safe for multithreaded use without external synchronization.

Unless otherwise mentioned, passing a null parameter into any method of a Scanner will cause a NullPointerException to be thrown.

A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the {@link #useRadix} method.

Localized numbers

An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the {@link java.util.Locale#getDefault} method; it may be changed via the {@link #useLocale} method.

The localized formats are defined in terms of the following parameters, which for a particular locale are taken from that locale's {@link java.text.DecimalFormat DecimalFormat} object, df, and its and {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} object, dfs.

LocalGroupSeparator   The character used to separate thousands groups, i.e., dfs.{@link java.text.DecimalFormatSymbols#getGroupingSeparator getGroupingSeparator()}
LocalDecimalSeparator   The character used for the decimal point, i.e., dfs.{@link java.text.DecimalFormatSymbols#getDecimalSeparator getDecimalSeparator()}
LocalPositivePrefix   The string that appears before a positive number (may be empty), i.e., df.{@link java.text.DecimalFormat#getPositivePrefix getPositivePrefix()}
LocalPositiveSuffix   The string that appears after a positive number (may be empty), i.e., df.{@link java.text.DecimalFormat#getPositiveSuffix getPositiveSuffix()}
LocalNegativePrefix   The string that appears before a negative number (may be empty), i.e., df.{@link java.text.DecimalFormat#getNegativePrefix getNegativePrefix()}
LocalNegativeSuffix   The string that appears after a negative number (may be empty), i.e., df.{@link java.text.DecimalFormat#getNegativeSuffix getNegativeSuffix()}
LocalNaN   The string that represents not-a-number for floating-point values, i.e., dfs.{@link java.text.DecimalFormatSymbols#getInfinity getInfinity()}
LocalInfinity   The string that represents infinity for floating-point values, i.e., dfs.{@link java.text.DecimalFormatSymbols#getInfinity getInfinity()}

Number syntax

The strings that can be parsed as numbers by an instance of this class are specified in terms of the following regular-expression grammar, where Rmax is the highest digit in the radix being used (for example, Rmax is 9 in base 10).

NonASCIIDigit  :: = A non-ASCII character c for which {@link java.lang.Character#isDigit Character.isDigit}(c) returns true
 
Non0Digit  :: = [1-Rmax] | NonASCIIDigit
 
Digit  :: = [0-Rmax] | NonASCIIDigit
 
GroupedNumeral  ::
= (  Non0Digit Digit? Digit?
LocalGroupSeparator Digit Digit Digit )+ )
 
Numeral  :: = ( ( Digit+ ) | GroupedNumeral )
 
Integer  :: = ( [-+]? ( Numeral ) )
| LocalPositivePrefix Numeral LocalPositiveSuffix
| LocalNegativePrefix Numeral LocalNegativeSuffix
 
DecimalNumeral  :: = Numeral
| Numeral LocalDecimalSeparator Digit*
| LocalDecimalSeparator Digit+
 
Exponent  :: = ( [eE] [+-]? Digit+ )
 
Decimal  :: = ( [-+]? DecimalNumeral Exponent? )
| LocalPositivePrefix DecimalNumeral LocalPositiveSuffix Exponent?
| LocalNegativePrefix DecimalNumeral LocalNegativeSuffix Exponent?
 
HexFloat  :: = [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)?
 
NonNumber  :: = NaN | LocalNan | Infinity | LocalInfinity
 
SignedNonNumber  :: = ( [-+]? NonNumber )
| LocalPositivePrefix NonNumber LocalPositiveSuffix
| LocalNegativePrefix NonNumber LocalNegativeSuffix
 
Float  :: = Decimal
| HexFloat
| SignedNonNumber

Whitespace is not significant in the above regular expressions.

version
1.15, 07/15/04
since
1.5

Fields Summary
private CharBuffer
buf
private static final int
BUFFER_SIZE
private int
position
private Matcher
matcher
private Pattern
delimPattern
private Pattern
hasNextPattern
private int
hasNextPosition
private String
hasNextResult
private Readable
source
private boolean
sourceClosed
private boolean
needInput
private boolean
skipped
private int
savedScannerPosition
private Object
typeCache
private boolean
matchValid
private boolean
closed
private int
radix
private int
defaultRadix
private Locale
locale
private sun.misc.LRUCache
patternCache
private IOException
lastException
private static Pattern
WHITESPACE_PATTERN
private static Pattern
FIND_ANY_PATTERN
private static Pattern
NON_ASCII_DIGIT
private String
groupSeparator
Locale dependent values used to scan numbers
private String
decimalSeparator
private String
nanString
private String
infinityString
private String
positivePrefix
private String
negativePrefix
private String
positiveSuffix
private String
negativeSuffix
private static volatile Pattern
boolPattern
Fields and an accessor method to match booleans
private static final String
BOOLEAN_PATTERN
private Pattern
integerPattern
Fields and methods to match bytes, shorts, ints, and longs
private String
digits
private String
non0Digit
private int
SIMPLE_GROUP_INDEX
private static volatile Pattern
separatorPattern
Fields and an accessor method to match line separators
private static final String
LINE_SEPARATOR_PATTERN
private Pattern
floatPattern
Fields and methods to match floats and doubles
private Pattern
decimalPattern
Constructors Summary
public Scanner(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 {@linkplain java.nio.charset.Charset#defaultCharset default charset}.

param
source An input stream to be scanned

        this(new InputStreamReader(source), WHITESPACE_PATTERN);
    
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.

param
source An input stream to be scanned
param
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

        this(makeReadable(source, charsetName), WHITESPACE_PATTERN);
    
public Scanner(File source)
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 {@linkplain java.nio.charset.Charset#defaultCharset default charset}.

param
source A file to be scanned
throws
FileNotFoundException if source is not found

        this((ReadableByteChannel)(new FileInputStream(source).getChannel())); 
    
public Scanner(File source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the specified charset.

param
source A file to be scanned
param
charsetName The encoding type used to convert bytes from the file into characters to be scanned
throws
FileNotFoundException if source is not found
throws
IllegalArgumentException if the specified encoding is not found

        this((ReadableByteChannel)(new FileInputStream(source).getChannel()), 
             charsetName);
    
public Scanner(String source)
Constructs a new Scanner that produces values scanned from the specified string.

param
source A string to scan

        this(new StringReader(source), WHITESPACE_PATTERN);
    
public Scanner(ReadableByteChannel source)
Constructs a new Scanner that produces values scanned from the specified channel. Bytes from the source are converted into characters using the underlying platform's {@linkplain java.nio.charset.Charset#defaultCharset default charset}.

param
source A channel to scan

        this(makeReadable(source), WHITESPACE_PATTERN);
    
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.

param
source A channel to scan
param
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

        this(makeReadable(source, charsetName), WHITESPACE_PATTERN);
    
private Scanner(Readable source, Pattern pattern)
Constructs a Scanner that returns values scanned from the specified source delimited by the specified pattern.

param
source A character source implementing the Readable interface
param
pattern A delimiting pattern
return
A scanner with the specified source and pattern

        if (source == null)
            throw new NullPointerException("source");
        if (pattern == null)
            throw new NullPointerException("pattern");
        this.source = source;
        delimPattern = pattern;
        buf = CharBuffer.allocate(BUFFER_SIZE);
        buf.limit(0);
        matcher = delimPattern.matcher(buf);
        matcher.useTransparentBounds(true);
        matcher.useAnchoringBounds(false);
        useLocale(Locale.getDefault());
    
public Scanner(Readable source)
Constructs a new Scanner that produces values scanned from the specified source.

param
source A character source implementing the {@link Readable} interface

        this(source, WHITESPACE_PATTERN);
    
Methods Summary
private static java.util.regex.PatternboolPattern()

        
        Pattern bp = boolPattern;
        if (bp == null)
            boolPattern = bp = Pattern.compile(BOOLEAN_PATTERN,
                                          Pattern.CASE_INSENSITIVE);
        return bp;
    
private voidbuildFloatAndDecimalPattern()

        // \\p{javaDigit} may not be perfect, see above
        String digit = "([0-9]|(\\p{javaDigit}))";
        String exponent = "([eE][+-]?"+digit+"+)?";
        String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
                                groupSeparator+digit+digit+digit+")+)";
        // Once again digit++ is used for performance, as above
        String numeral = "(("+digit+"++)|"+groupedNumeral+")";
        String decimalNumeral = "("+numeral+"|"+numeral + 
            decimalSeparator + digit + "*+|"+ decimalSeparator + 
            digit + "++)";
        String nonNumber = "(NaN|"+nanString+"|Infinity|"+
                               infinityString+")";
        String positiveFloat = "(" + positivePrefix + decimalNumeral + 
                            positiveSuffix + exponent + ")";
        String negativeFloat = "(" + negativePrefix + decimalNumeral + 
                            negativeSuffix + exponent + ")";
        String decimal = "(([-+]?" + decimalNumeral + exponent + ")|"+ 
            positiveFloat + "|" + negativeFloat + ")";
        String hexFloat = 
            "[-+]?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP][-+]?[0-9]+)?";
        String positiveNonNumber = "(" + positivePrefix + nonNumber + 
                            positiveSuffix + ")";
        String negativeNonNumber = "(" + negativePrefix + nonNumber + 
                            negativeSuffix + ")";
        String signedNonNumber = "(([-+]?"+nonNumber+")|" +
                                 positiveNonNumber + "|" + 
                                 negativeNonNumber + ")";
        floatPattern = Pattern.compile(decimal + "|" + hexFloat + "|" +
                                       signedNonNumber);
        decimalPattern = Pattern.compile(decimal);
    
private java.lang.StringbuildIntegerPatternString()

       
        String radixDigits = digits.substring(0, radix);
        // \\p{javaDigit} is not guaranteed to be appropriate
        // here but what can we do? The final authority will be
        // whatever parse method is invoked, so ultimately the
        // Scanner will do the right thing
        String digit = "((?i)["+radixDigits+"]|\\p{javaDigit})";
        String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
                                groupSeparator+digit+digit+digit+")+)";
        // digit++ is the possessive form which is necessary for reducing
        // backtracking that would otherwise cause unacceptable performance
        String numeral = "(("+ digit+"++)|"+groupedNumeral+")";
        String javaStyleInteger = "([-+]?(" + numeral + "))";
        String negativeInteger = negativePrefix + numeral + negativeSuffix;
        String positiveInteger = positivePrefix + numeral + positiveSuffix;
        return "("+ javaStyleInteger + ")|(" + 
            positiveInteger + ")|(" + 
            negativeInteger + ")";
    
private voidcacheResult(java.util.regex.Pattern p)

        hasNextPattern = p;
        hasNextResult = matcher.group();
        hasNextPosition = matcher.end();
    
private voidclearCaches()

        hasNextPattern = null;
        typeCache = null;
    
public voidclose()
Closes this scanner.

If this scanner has not yet been closed then if its underlying {@linkplain java.lang.Readable readable} also implements the {@link java.io.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 {@link IllegalStateException}.

        if (closed)
            return;
        if (source instanceof Closeable) {
            try {
                ((Closeable)source).close();
            } catch (IOException ioe) {
                lastException = ioe;
            }
        }
        sourceClosed = true;
        source = null;
        closed = true;
    
private java.util.regex.PatterndecimalPattern()

        if (decimalPattern == null) {
            buildFloatAndDecimalPattern();
        }
        return decimalPattern;
    
public java.util.regex.Patterndelimiter()
Returns the Pattern this Scanner is currently using to match delimiters.

return
this scanner's delimiting pattern.

        return delimPattern;
    
private voidensureOpen()

        if (closed)
            throw new IllegalStateException("Scanner closed");
    
public java.lang.StringfindInLine(java.lang.String pattern)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.

An invocation of this method of the form findInLine(pattern) behaves in exactly the same way as the invocation findInLine(Pattern.compile(pattern)).

param
pattern a string specifying the pattern to search for
return
the text that matched the specified pattern
throws
IllegalStateException if this scanner is closed

        return findInLine(patternCache.forName(pattern));
    
public java.lang.StringfindInLine(java.util.regex.Pattern pattern)
Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.

Since this method continues to search through the input looking for the specified pattern, it may buffer all of the input searching for the desired token if no line separators are present.

param
pattern the pattern to scan for
return
the text that matched the specified pattern
throws
IllegalStateException if this scanner is closed

        ensureOpen();
        if (pattern == null)
            throw new NullPointerException();
        clearCaches();

        // Expand buffer to include the next newline or end of input
        int endPosition = 0;
        saveState();
        while (true) {
            String token = findPatternInBuffer(separatorPattern(), 0);
            if (token != null) {
                endPosition = matcher.start();
                break; // up to next newline
            }
            if (needInput) {
                readInput();
            } else {
                endPosition = buf.limit();
                break; // up to end of input
            }
        }
        revertState();
        int horizonForLine = endPosition - position;

        // Search for the pattern
        return findWithinHorizon(pattern, horizonForLine);
    
private java.lang.StringfindPatternInBuffer(java.util.regex.Pattern pattern, int horizon)

        matchValid = false;
        matcher.usePattern(pattern);
        int bufferLimit = buf.limit();
        int horizonLimit = -1;
        int searchLimit = bufferLimit;
        if (horizon > 0) {
            horizonLimit = position + horizon;
            if (horizonLimit < bufferLimit)
                searchLimit = horizonLimit;
        }
        matcher.region(position, searchLimit);
        if (matcher.find()) {
            if (matcher.hitEnd() && (!sourceClosed)) {
                // The match may be longer if didn't hit horizon or real end
                if (searchLimit != horizonLimit) {
                     // Hit an artificial end; try to extend the match
                    needInput = true;
                    return null;
                }
                // The match could go away depending on what is next
                if ((searchLimit == horizonLimit) && matcher.requireEnd()) {
                    // Rare case: we hit the end of input and it happens
                    // that it is at the horizon and the end of input is 
                    // required for the match.
                    needInput = true;
                    return null;
                }
            }
            // Did not hit end, or hit real end, or hit horizon
            position = matcher.end();
            return matcher.group();
        }

        if (sourceClosed)
            return null;

        // If there is no specified horizon, or if we have not searched
        // to the specified horizon yet, get more input
        if ((horizon == 0) || (searchLimit != horizonLimit))
            needInput = true;
        return null;
    
public java.lang.StringfindWithinHorizon(java.lang.String pattern, int horizon)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.

An invocation of this method of the form findWithinHorizon(pattern) behaves in exactly the same way as the invocation findWithinHorizon(Pattern.compile(pattern, horizon)).

param
pattern a string specifying the pattern to search for
return
the text that matched the specified pattern
throws
IllegalStateException if this scanner is closed
throws
IllegalArgumentException if horizon is negative

        return findWithinHorizon(patternCache.forName(pattern), horizon);
    
public java.lang.StringfindWithinHorizon(java.util.regex.Pattern pattern, int horizon)
Attempts to find the next occurrence of the specified pattern.

This method searches through the input up to the specified search horizon, ignoring delimiters. If the pattern is found the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected then the null is returned and the scanner's position remains unchanged. This method may block waiting for input that matches the pattern.

A scanner will never search more than horizon code points beyond its current position. Note that a match may be clipped by the horizon; that is, an arbitrary match result may have been different if the horizon had been larger. The scanner treats the horizon as a transparent, non-anchoring bound (see {@link Matcher#useTransparentBounds} and {@link Matcher#useAnchoringBounds}).

If horizon is 0, then the horizon is ignored and this method continues to search through the input looking for the specified pattern without bound. In this case it may buffer all of the input searching for the pattern.

If horizon is negative, then an IllegalArgumentException is thrown.

param
pattern the pattern to scan for
return
the text that matched the specified pattern
throws
IllegalStateException if this scanner is closed
throws
IllegalArgumentException if horizon is negative

        ensureOpen();
        if (pattern == null)
            throw new NullPointerException();
        if (horizon < 0)
            throw new IllegalArgumentException("horizon < 0");
        clearCaches();

        // Search for the pattern
        while (true) {
            String token = findPatternInBuffer(pattern, horizon);
            if (token != null) {
                matchValid = true;
                return token;
            }
            if (needInput)
                readInput();
            else
                break; // up to end of input
        }
        return null;
    
private java.util.regex.PatternfloatPattern()

        if (floatPattern == null) {
            buildFloatAndDecimalPattern();
        }
        return floatPattern;
    
private java.lang.StringgetCachedResult()

        position = hasNextPosition;
        hasNextPattern = null;
        typeCache = null;
        return hasNextResult;
    
private java.lang.StringgetCompleteTokenInBuffer(java.util.regex.Pattern pattern)

        matchValid = false;

        // Skip delims first
        matcher.usePattern(delimPattern);
        if (!skipped) { // Enforcing only one skip of leading delims
            matcher.region(position, buf.limit());
            if (matcher.lookingAt()) {
                // If more input could extend the delimiters then we must wait 
                // for more input
                if (matcher.hitEnd() && !sourceClosed) {
                    needInput = true;
                    return null;
                }
                // The delims were whole and the matcher should skip them
                skipped = true;
                position = matcher.end();
            }
        }

        // If we are sitting at the end, no more tokens in buffer
        if (position == buf.limit()) {
            if (sourceClosed)
                return null;
            needInput = true;
            return null;
        }

        // Must look for next delims. Simply attempting to match the
        // pattern at this point may find a match but it might not be
        // the first longest match because of missing input, or it might
        // match a partial token instead of the whole thing.

        // Then look for next delims
        matcher.region(position, buf.limit());
        boolean foundNextDelim = matcher.find();
        if (foundNextDelim && (matcher.end() == position)) {
            // Zero length delimiter match; we should find the next one
            // using the automatic advance past a zero length match;
            // Otherwise we have just found the same one we just skipped
            foundNextDelim = matcher.find();
        }
        if (foundNextDelim) {
            // In the rare case that more input could cause the match
            // to be lost and there is more input coming we must wait 
            // for more input. Note that hitting the end is okay as long
            // as the match cannot go away. It is the beginning of the
            // next delims we want to be sure about, we don't care if
            // they potentially extend further.
            if (matcher.requireEnd() && !sourceClosed) {
                needInput = true;
                return null;
            }
            int tokenEnd = matcher.start();
            // There is a complete token.
            if (pattern == null) {
                // Must continue with match to provide valid MatchResult
                pattern = FIND_ANY_PATTERN;
            }
            //  Attempt to match against the desired pattern
            matcher.usePattern(pattern);
            matcher.region(position, tokenEnd);
            if (matcher.matches()) {
                String s = matcher.group();
                position = matcher.end();
                return s;
            } else { // Complete token but it does not match
                return null;
            }
        }

        // If we can't find the next delims but no more input is coming,
        // then we can treat the remainder as a whole token
        if (sourceClosed) {
            if (pattern == null) {
                // Must continue with match to provide valid MatchResult
                pattern = FIND_ANY_PATTERN;
            }
            // Last token; Match the pattern here or throw
            matcher.usePattern(pattern);
            matcher.region(position, buf.limit());
            if (matcher.matches()) {
                String s = matcher.group();
                position = matcher.end();
                return s;
            }
            // Last piece does not match
            return null;
        }

        // There is a partial token in the buffer; must read more
        // to complete it
        needInput = true;
        return null;
    
public booleanhasNext()
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.

return
true if and only if this scanner has another token
throws
IllegalStateException if this scanner is closed
see
java.util.Iterator

        ensureOpen();
        saveState();
        while (!sourceClosed) {
            if (hasTokenInBuffer())
                return revertState(true);
            readInput();
        }
        boolean result = hasTokenInBuffer();
        return revertState(result);
    
public booleanhasNext(java.lang.String pattern)
Returns true if the next token matches the pattern constructed from the specified string. The scanner does not advance past any input.

An invocation of this method of the form hasNext(pattern) behaves in exactly the same way as the invocation hasNext(Pattern.compile(pattern)).

param
pattern a string specifying the pattern to scan
return
true if and only if this scanner has another token matching the specified pattern
throws
IllegalStateException if this scanner is closed

        return hasNext(patternCache.forName(pattern));
    
public booleanhasNext(java.util.regex.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.

param
pattern the pattern to scan for
return
true if and only if this scanner has another token matching the specified pattern
throws
IllegalStateException if this scanner is closed

        ensureOpen();
        if (pattern == null)
            throw new NullPointerException();
        hasNextPattern = null;
        saveState();

        while (true) {
            if (getCompleteTokenInBuffer(pattern) != null) {
                matchValid = true;
                cacheResult(pattern);
                return revertState(true);
            }
            if (needInput)
                readInput();
            else
                return revertState(false);
        }
    
public booleanhasNextBigDecimal()
Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the {@link #nextBigDecimal} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid BigDecimal
throws
IllegalStateException if this scanner is closed

        setRadix(10);
        boolean result = hasNext(decimalPattern());
        if (result) { // Cache it
            try {
                String s = processFloatToken(hasNextResult);
                typeCache = new BigDecimal(s);
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextBigInteger()
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the {@link #nextBigInteger} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid BigInteger
throws
IllegalStateException if this scanner is closed

        return hasNextBigInteger(defaultRadix);
    
public booleanhasNextBigInteger(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 {@link #nextBigInteger} method. The scanner does not advance past any input.

param
radix the radix used to interpret the token as an integer
return
true if and only if this scanner's next token is a valid BigInteger
throws
IllegalStateException if this scanner is closed

        setRadix(radix);
        boolean result = hasNext(integerPattern());
        if (result) { // Cache it
            try {
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
                    processIntegerToken(hasNextResult) :
                    hasNextResult;
                typeCache = new BigInteger(s, radix);
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextBoolean()
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.

return
true if and only if this scanner's next token is a valid boolean value
throws
IllegalStateException if this scanner is closed

        return hasNext(boolPattern());
    
public booleanhasNextByte()
Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the {@link #nextByte} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid byte value
throws
IllegalStateException if this scanner is closed

        return hasNextByte(defaultRadix);
    
public booleanhasNextByte(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 {@link #nextByte} method. The scanner does not advance past any input.

param
radix the radix used to interpret the token as a byte value
return
true if and only if this scanner's next token is a valid byte value
throws
IllegalStateException if this scanner is closed

        setRadix(radix);
        boolean result = hasNext(integerPattern());
        if (result) { // Cache it
            try {
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
                    processIntegerToken(hasNextResult) :
                    hasNextResult;
                typeCache = Byte.parseByte(s, radix);
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextDouble()
Returns true if the next token in this scanner's input can be interpreted as a double value using the {@link #nextDouble} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid double value
throws
IllegalStateException if this scanner is closed

        setRadix(10);
        boolean result = hasNext(floatPattern());
        if (result) { // Cache it
            try {
                String s = processFloatToken(hasNextResult);
                typeCache = Double.valueOf(Double.parseDouble(s));
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextFloat()
Returns true if the next token in this scanner's input can be interpreted as a float value using the {@link #nextFloat} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid float value
throws
IllegalStateException if this scanner is closed

        setRadix(10);
        boolean result = hasNext(floatPattern());
        if (result) { // Cache it
            try {
                String s = processFloatToken(hasNextResult);
                typeCache = Float.valueOf(Float.parseFloat(s));
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextInt()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the {@link #nextInt} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid int value
throws
IllegalStateException if this scanner is closed

        return hasNextInt(defaultRadix);
    
public booleanhasNextInt(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 {@link #nextInt} method. The scanner does not advance past any input.

param
radix the radix used to interpret the token as an int value
return
true if and only if this scanner's next token is a valid int value
throws
IllegalStateException if this scanner is closed

        setRadix(radix);
        boolean result = hasNext(integerPattern());
        if (result) { // Cache it
            try {
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
                    processIntegerToken(hasNextResult) :
                    hasNextResult;
                typeCache = Integer.parseInt(s, radix);
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextLine()
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.

return
true if and only if this scanner has another line of input
throws
IllegalStateException if this scanner is closed

        saveState();
        String result = findWithinHorizon(
            ".*("+LINE_SEPARATOR_PATTERN+")|.+$", 0);
        revertState();
        return (result != null);
    
public booleanhasNextLong()
Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the {@link #nextLong} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid long value
throws
IllegalStateException if this scanner is closed

        return hasNextLong(defaultRadix);
    
public booleanhasNextLong(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 {@link #nextLong} method. The scanner does not advance past any input.

param
radix the radix used to interpret the token as a long value
return
true if and only if this scanner's next token is a valid long value
throws
IllegalStateException if this scanner is closed

        setRadix(radix);
        boolean result = hasNext(integerPattern());
        if (result) { // Cache it
            try {
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
                    processIntegerToken(hasNextResult) :
                    hasNextResult;
                typeCache = Long.parseLong(s, radix);
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
public booleanhasNextShort()
Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the {@link #nextShort} method. The scanner does not advance past any input.

return
true if and only if this scanner's next token is a valid short value in the default radix
throws
IllegalStateException if this scanner is closed

        return hasNextShort(defaultRadix);
    
public booleanhasNextShort(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 {@link #nextShort} method. The scanner does not advance past any input.

param
radix the radix used to interpret the token as a short value
return
true if and only if this scanner's next token is a valid short value in the specified radix
throws
IllegalStateException if this scanner is closed

        setRadix(radix);
        boolean result = hasNext(integerPattern());
        if (result) { // Cache it
            try {
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
                    processIntegerToken(hasNextResult) :
                    hasNextResult;
                typeCache = Short.parseShort(s, radix);
            } catch (NumberFormatException nfe) {
                result = false;
            }
        }
        return result;
    
private booleanhasTokenInBuffer()

        matchValid = false;
        matcher.usePattern(delimPattern);
        matcher.region(position, buf.limit());

        // Skip delims first
        if (matcher.lookingAt())
            position = matcher.end();

        // If we are sitting at the end, no more tokens in buffer
        if (position == buf.limit())
            return false;

        return true;
    
private java.util.regex.PatternintegerPattern()

        if (integerPattern == null) {
            integerPattern = patternCache.forName(buildIntegerPatternString());
        }
        return integerPattern;
    
public java.io.IOExceptionioException()
Returns the IOException last thrown by this Scanner's underlying Readable. This method returns null if no such exception exists.

return
the last exception thrown by this scanner's readable

        return lastException;
    
public java.util.Localelocale()
Returns this scanner's locale.

A scanner's locale affects many elements of its default primitive matching regular expressions; see localized numbers above.

return
this scanner's locale

        return this.locale;
    
private static java.lang.ReadablemakeReadable(java.io.InputStream source, java.lang.String charsetName)

        if (source == null)
            throw new NullPointerException("source");
        InputStreamReader isr = null;
        try {
            isr = new InputStreamReader(source, charsetName);
        } catch (UnsupportedEncodingException uee) {
            IllegalArgumentException iae = new IllegalArgumentException();
            iae.initCause(uee);
            throw iae;
        }
        return isr;
    
private static java.lang.ReadablemakeReadable(java.nio.channels.ReadableByteChannel source)

        if (source == null)
            throw new NullPointerException("source");
        String defaultCharsetName = 
            java.nio.charset.Charset.defaultCharset().name();
        return Channels.newReader(source, 
                           java.nio.charset.Charset.defaultCharset().name());
    
private static java.lang.ReadablemakeReadable(java.nio.channels.ReadableByteChannel source, java.lang.String charsetName)

        if (source == null)
            throw new NullPointerException("source");
        if (!Charset.isSupported(charsetName))
            throw new IllegalArgumentException(charsetName);
        return Channels.newReader(source, charsetName);
    
private booleanmakeSpace()

        clearCaches();
        int offset = savedScannerPosition == -1 ? 
            position : savedScannerPosition;
        buf.position(offset);
        // Gain space by compacting buffer
        if (offset > 0) {
            buf.compact();
            translateSavedIndexes(offset);
            position -= offset;
            buf.flip();
            return true;
        }
        // Gain space by growing buffer
        int newSize = buf.capacity() * 2;
        CharBuffer newBuf = CharBuffer.allocate(newSize);
        newBuf.put(buf);
        newBuf.flip();
        translateSavedIndexes(offset);
        position -= offset;
        buf = newBuf;
        matcher.reset(buf);
        return true;
    
public java.util.regex.MatchResultmatch()
Returns the match result of the last scanning operation performed by this scanner. This method throws IllegalStateException if no match has been performed, or if the last match was not successful.

The various nextmethods of Scanner make a match result available if they complete without throwing an exception. For instance, after an invocation of the {@link #nextInt} method that returned an int, this method returns a MatchResult for the search of the Integer regular expression defined above. Similarly the {@link #findInLine}, {@link #findWithinHorizon}, and {@link #skip} methods will make a match available if they succeed.

return
a match result for the last match operation
throws
IllegalStateException If no match result is available

        if (!matchValid)
            throw new IllegalStateException("No match result available");
        return matcher.toMatchResult();
    
private java.lang.StringmatchPatternInBuffer(java.util.regex.Pattern pattern)

        matchValid = false;
        matcher.usePattern(pattern);
        matcher.region(position, buf.limit());
        if (matcher.lookingAt()) {
            if (matcher.hitEnd() && (!sourceClosed)) {
                // Get more input and try again
                needInput = true;
                return null;
            }
            position = matcher.end();
            return matcher.group();
        }

        if (sourceClosed)
            return null;

        // Read more to find pattern
        needInput = true;
        return null;
    
public java.lang.Stringnext()
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 {@link #hasNext} returned true.

return
the next token
throws
NoSuchElementException if no more tokens are available
throws
IllegalStateException if this scanner is closed
see
java.util.Iterator

        ensureOpen();
        clearCaches();
        
        while (true) {
            String token = getCompleteTokenInBuffer(null);
            if (token != null) {
                matchValid = true;
                skipped = false;
                return token;
            }
            if (needInput)
                readInput();
            else
                throwFor();
        }
    
public java.lang.Stringnext(java.lang.String pattern)
Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.

An invocation of this method of the form next(pattern) behaves in exactly the same way as the invocation next(Pattern.compile(pattern)).

param
pattern a string specifying the pattern to scan
return
the next token
throws
NoSuchElementException if no such tokens are available
throws
IllegalStateException if this scanner is closed

        return next(patternCache.forName(pattern));
    
public java.lang.Stringnext(java.util.regex.Pattern pattern)
Returns the next token if it matches the specified pattern. This method may block while waiting for input to scan, even if a previous invocation of {@link #hasNext(Pattern)} returned true. If the match is successful, the scanner advances past the input that matched the pattern.

param
pattern the pattern to scan for
return
the next token
throws
NoSuchElementException if no more tokens are available
throws
IllegalStateException if this scanner is closed

        ensureOpen();
        if (pattern == null)
            throw new NullPointerException();

        // Did we already find this pattern?
        if (hasNextPattern == pattern)
            return getCachedResult();
        clearCaches();

        // Search for the pattern
        while (true) {
            String token = getCompleteTokenInBuffer(pattern);
            if (token != null) {
                matchValid = true;
                skipped = false;
                return token;
            }
            if (needInput)
                readInput();
            else
                throwFor();
        }
    
public java.math.BigDecimalnextBigDecimal()
Scans the next token of the input as a {@link java.math.BigDecimal BigDecimal}.

If the next token matches the Decimal regular expression defined above then the token is converted into a BigDecimal value as if by removing all group separators, mapping non-ASCII digits into ASCII digits via the {@link Character#digit Character.digit}, and passing the resulting string to the {@link java.math.BigDecimal#BigDecimal(java.lang.String) BigDecimal(String)} constructor.

return
the BigDecimal scanned from the input
throws
InputMismatchException if the next token does not match the Decimal regular expression, or is out of range
throws
NoSuchElementException if the input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof BigDecimal)) {
            BigDecimal val = (BigDecimal)typeCache;
            useTypeCache();
            return val;
        }
        setRadix(10);
        clearCaches();
        // Search for next float
        try {
            String s = processFloatToken(next(decimalPattern()));
            return new BigDecimal(s);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public java.math.BigIntegernextBigInteger()
Scans the next token of the input as a {@link java.math.BigInteger BigInteger}.

An invocation of this method of the form nextBigInteger() behaves in exactly the same way as the invocation nextBigInteger(radix), where radix is the default radix of this scanner.

return
the BigInteger scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if the input is exhausted
throws
IllegalStateException if this scanner is closed

        return nextBigInteger(defaultRadix);
    
public java.math.BigIntegernextBigInteger(int radix)
Scans the next token of the input as a {@link java.math.BigInteger BigInteger}.

If the next token matches the Integer regular expression defined above then the token is converted into a BigInteger value as if by removing all group separators, mapping non-ASCII digits into ASCII digits via the {@link Character#digit Character.digit}, and passing the resulting string to the {@link java.math.BigInteger#BigInteger(java.lang.String) BigInteger(String, int)} constructor with the specified radix.

param
radix the radix used to interpret the token
return
the BigInteger scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if the input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof BigInteger)) {
            BigInteger val = (BigInteger)typeCache;
            useTypeCache();
            return val;
        }
        setRadix(radix);
        clearCaches();
        // Search for next int
        try {
            String s = next(integerPattern());
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                s = processIntegerToken(s);
            return new BigInteger(s, radix);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public booleannextBoolean()
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.

return
the boolean scanned from the input
throws
InputMismatchException if the next token is not a valid boolean
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

        clearCaches();
        return Boolean.parseBoolean(next(boolPattern()));
    
public bytenextByte()
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.

return
the byte scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

         return nextByte(defaultRadix);
    
public bytenextByte(int radix)
Scans the next token of the input as a byte. This method will throw InputMismatchException if the next token cannot be translated into a valid byte value as described below. If the translation is successful, the scanner advances past the input that matched.

If the next token matches the Integer regular expression defined above then the token is converted into a byte value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via {@link Character#digit Character.digit}, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to {@link Byte#parseByte(String, int) Byte.parseByte} with the specified radix.

param
radix the radix used to interpret the token as a byte value
return
the byte scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Byte)) {
            byte val = ((Byte)typeCache).byteValue();
            useTypeCache();
            return val;
        }
        setRadix(radix);
        clearCaches();
        // Search for next byte
        try {
            String s = next(integerPattern());
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                s = processIntegerToken(s);
            return Byte.parseByte(s, radix);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public doublenextDouble()
Scans the next token of the input as a double. This method will throw InputMismatchException 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.

If the next token matches the Float regular expression defined above then the token is converted into a double value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via {@link Character#digit Character.digit}, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to {@link Double#parseDouble Double.parseDouble}. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed to {@link Double#parseDouble(String) Double.parseDouble} as appropriate.

return
the double scanned from the input
throws
InputMismatchException if the next token does not match the Float regular expression, or is out of range
throws
NoSuchElementException if the input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Double)) {
            double val = ((Double)typeCache).doubleValue();
            useTypeCache();
            return val;
        }
        setRadix(10);
        clearCaches();
        // Search for next float
        try {
            return Double.parseDouble(processFloatToken(next(floatPattern())));
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public floatnextFloat()
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 {@link Character#digit Character.digit}, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to {@link Float#parseFloat Float.parseFloat}. If the token matches the localized NaN or infinity strings, then either "Nan" or "Infinity" is passed to {@link Float#parseFloat(String) Float.parseFloat} as appropriate.

return
the float scanned from the input
throws
InputMismatchException if the next token does not match the Float regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Float)) {
            float val = ((Float)typeCache).floatValue();
            useTypeCache();
            return val;
        }
        setRadix(10);
        clearCaches();
        try {
            return Float.parseFloat(processFloatToken(next(floatPattern())));
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public intnextInt()
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.

return
the int scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

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

If the next token matches the Integer regular expression defined above then the token is converted into an int value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via {@link Character#digit Character.digit}, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to {@link Integer#parseInt(String, int) Integer.parseInt} with the specified radix.

param
radix the radix used to interpret the token as an int value
return
the int scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Integer)) {
            int val = ((Integer)typeCache).intValue();
            useTypeCache();
            return val;
        }
        setRadix(radix);
        clearCaches();
        // Search for next int
        try {
            String s = next(integerPattern());
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                s = processIntegerToken(s);
            return Integer.parseInt(s, radix);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public java.lang.StringnextLine()
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.

return
the line that was skipped
throws
NoSuchElementException if no line was found
throws
IllegalStateException if this scanner is closed

        String result = findWithinHorizon(
            ".*("+LINE_SEPARATOR_PATTERN+")|.+$", 0);
        if (result == null)
            throw new NoSuchElementException("No line found");
        MatchResult mr = this.match();
        String lineSep = mr.group(1);
        if (lineSep != null)
            result = result.substring(0, result.length() - lineSep.length());
        if (result == null)
            throw new NoSuchElementException();
        else
            return result;
    
public longnextLong()
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.

return
the long scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

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

If the next token matches the Integer regular expression defined above then the token is converted into an long value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via {@link Character#digit Character.digit}, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to {@link Long#parseLong(String, int) Long.parseLong} with the specified radix.

param
radix the radix used to interpret the token as an int value
return
the long scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Long)) {
            long val = ((Long)typeCache).longValue();
            useTypeCache();
            return val;
        }
        setRadix(radix);
        clearCaches();
        try {
            String s = next(integerPattern());
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                s = processIntegerToken(s);
            return Long.parseLong(s, radix);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
public shortnextShort()
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.

return
the short scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

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

If the next token matches the Integer regular expression defined above then the token is converted into a short value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via {@link Character#digit Character.digit}, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to {@link Short#parseShort(String, int) Short.parseShort} with the specified radix.

param
radix the radix used to interpret the token as a short value
return
the short scanned from the input
throws
InputMismatchException if the next token does not match the Integer regular expression, or is out of range
throws
NoSuchElementException if input is exhausted
throws
IllegalStateException if this scanner is closed

        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Short)) {
            short val = ((Short)typeCache).shortValue();
            useTypeCache();
            return val;
        }
        setRadix(radix);
        clearCaches();
        // Search for next short
        try {
            String s = next(integerPattern());
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                s = processIntegerToken(s);
            return Short.parseShort(s, radix);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
            throw new InputMismatchException(nfe.getMessage());
        }
    
private java.lang.StringprocessFloatToken(java.lang.String token)
The float token must be stripped of prefixes, group separators, and suffixes, non ascii digits must be converted into ascii digits before parseFloat will accept it. If there are non-ascii digits in the token these digits must be processed before the token is passed to parseFloat.

        String result = token.replaceAll(groupSeparator, "");
        if (!decimalSeparator.equals("\\."))
            result = result.replaceAll(decimalSeparator, ".");
        boolean isNegative = false;
        int preLen = negativePrefix.length();
        if ((preLen > 0) && result.startsWith(negativePrefix)) {
            isNegative = true;
            result = result.substring(preLen);
        }
        int sufLen = negativeSuffix.length();
        if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
            isNegative = true;
            result = result.substring(result.length() - sufLen,
                                      result.length());
        }
        if (result.equals(nanString))
            result = "NaN";
        if (result.equals(infinityString))
            result = "Infinity";
        if (isNegative)
            result = "-" + result;

        // Translate non-ASCII digits
        Matcher m = NON_ASCII_DIGIT.matcher(result);
        if (m.find()) {
            StringBuilder inASCII = new StringBuilder();
            for (int i=0; i<result.length(); i++) { 
                char nextChar = result.charAt(i);
                if (Character.isDigit(nextChar)) {
                    int d = Character.digit(nextChar, 10);
                    if (d != -1)
                        inASCII.append(d);
                    else
                        inASCII.append(nextChar);
                } else {
                    inASCII.append(nextChar);
                }
            }
            result = inASCII.toString();
        }

        return result;
    
private java.lang.StringprocessIntegerToken(java.lang.String token)
The integer token must be stripped of prefixes, group separators, and suffixes, non ascii digits must be converted into ascii digits before parse will accept it.

        String result = token.replaceAll(""+groupSeparator, "");
        boolean isNegative = false;
        int preLen = negativePrefix.length();
        if ((preLen > 0) && result.startsWith(negativePrefix)) {
            isNegative = true;
            result = result.substring(preLen);
        }
        int sufLen = negativeSuffix.length();
        if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
            isNegative = true;
            result = result.substring(result.length() - sufLen,
                                      result.length());
        }
        if (isNegative)
            result = "-" + result;
        return result;
    
public intradix()
Returns this scanner's default radix.

A scanner's radix affects elements of its default number matching regular expressions; see localized numbers above.

return
the default radix of this scanner

        return this.defaultRadix;
    
private voidreadInput()

        if (buf.limit() == buf.capacity())
            makeSpace();

        // Prepare to receive data
        int p = buf.position();
        buf.position(buf.limit());
        buf.limit(buf.capacity());

        int n = 0;
        try {
            n = source.read(buf);
        } catch (IOException ioe) {
            lastException = ioe;
            n = -1;
        }

        if (n == -1) {
            sourceClosed = true;
            needInput = false;
        }

        if (n > 0)
            needInput = false;

        // Restore current position and limit for reading
        buf.limit(buf.position());
        buf.position(p);
    
public voidremove()
The remove operation is not supported by this implementation of Iterator.

throws
UnsupportedOperationException if this method is invoked.
see
java.util.Iterator

        throw new UnsupportedOperationException();
    
private voidrevertState()

        this.position = savedScannerPosition;
        savedScannerPosition = -1;
        skipped = false;
    
private booleanrevertState(boolean b)

        this.position = savedScannerPosition;
        savedScannerPosition = -1;
        skipped = false;
        return b;
    
private voidsaveState()

        savedScannerPosition = position;
    
private static java.util.regex.PatternseparatorPattern()

        
        Pattern sp = separatorPattern;
        if (sp == null)
            separatorPattern = sp = Pattern.compile(LINE_SEPARATOR_PATTERN);
        return sp;
    
private voidsetRadix(int radix)

        if (this.radix != radix) {
            // Force rebuilding and recompilation of radix dependent patterns
            integerPattern = null;
            this.radix = radix;
        }
    
public java.util.Scannerskip(java.util.regex.Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters. This method will skip input if an anchored match of the specified pattern succeeds.

If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown.

Since this method seeks to match the specified pattern starting at the scanner's current position, patterns that can match a lot of input (".*", for example) may cause the scanner to buffer a large amount of input.

Note that it is possible to skip something without risking a NoSuchElementException by using a pattern that can match nothing, e.g., sc.skip("[ \t]*").

param
pattern a string specifying the pattern to skip over
return
this scanner
throws
NoSuchElementException if the specified pattern is not found
throws
IllegalStateException if this scanner is closed

        ensureOpen();
        if (pattern == null)
            throw new NullPointerException();
        clearCaches();

        // Search for the pattern
        while (true) {
            String token = matchPatternInBuffer(pattern);
            if (token != null) {
                matchValid = true;
                position = matcher.end();
                return this;
            }
            if (needInput)
                readInput();
            else
                throw new NoSuchElementException();
        }
    
public java.util.Scannerskip(java.lang.String pattern)
Skips input that matches a pattern constructed from the specified string.

An invocation of this method of the form skip(pattern) behaves in exactly the same way as the invocation skip(Pattern.compile(pattern)).

param
pattern a string specifying the pattern to skip over
return
this scanner
throws
IllegalStateException if this scanner is closed

        return skip(patternCache.forName(pattern));
    
private voidthrowFor()

        skipped = false;
        if ((sourceClosed) && (position == buf.limit()))
            throw new NoSuchElementException();
        else
            throw new InputMismatchException();
    
public java.lang.StringtoString()

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.

return
The string representation of this scanner

        StringBuilder sb = new StringBuilder();
	sb.append("java.util.Scanner");
	sb.append("[delimiters=" + delimPattern + "]");
	sb.append("[position=" + position + "]");
	sb.append("[match valid=" + matchValid + "]");
	sb.append("[need input=" + needInput + "]");
	sb.append("[source closed=" + sourceClosed + "]");
        sb.append("[skipped=" + skipped + "]");
        sb.append("[group separator=" + groupSeparator + "]");
        sb.append("[decimal separator=" + decimalSeparator + "]");
        sb.append("[positive prefix=" + positivePrefix + "]");
	sb.append("[negative prefix=" + negativePrefix + "]");
	sb.append("[positive suffix=" + positiveSuffix + "]");
	sb.append("[negative suffix=" + negativeSuffix + "]");
	sb.append("[NaN string=" + nanString + "]");
	sb.append("[infinity string=" + infinityString + "]");
	return sb.toString();
    
private voidtranslateSavedIndexes(int offset)

        if (savedScannerPosition != -1)
            savedScannerPosition -= offset;
    
public java.util.ScanneruseDelimiter(java.util.regex.Pattern pattern)
Sets this scanner's delimiting pattern to the specified pattern.

param
pattern A delimiting pattern
return
this scanner

        delimPattern = pattern;
        return this;
    
public java.util.ScanneruseDelimiter(java.lang.String pattern)
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.

An invocation of this method of the form useDelimiter(pattern) behaves in exactly the same way as the invocation hasDelimiter(Pattern.compile(pattern)).

param
pattern A string specifying a delimiting pattern
return
this scanner

        delimPattern = patternCache.forName(pattern);
        return this;
    
public java.util.ScanneruseLocale(java.util.Locale locale)
Sets this scanner's locale to the specified locale.

A scanner's locale affects many elements of its default primitive matching regular expressions; see localized numbers above.

param
locale A string specifying the locale to use
return
this scanner

        if (locale.equals(this.locale))
            return this;

        this.locale = locale;
        DecimalFormat df = 
            (DecimalFormat)NumberFormat.getNumberInstance(locale);
        DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);

        // These must be literalized to avoid collision with regex
        // metacharacters such as dot or parenthesis
        groupSeparator =   "\\" + dfs.getGroupingSeparator();
        decimalSeparator = "\\" + dfs.getDecimalSeparator();

        // Quoting the nonzero length locale-specific things
        // to avoid potential conflict with metacharacters
        nanString = "\\Q" + dfs.getNaN() + "\\E";
        infinityString = "\\Q" + dfs.getInfinity() + "\\E";
        positivePrefix = df.getPositivePrefix();
        if (positivePrefix.length() > 0)
            positivePrefix = "\\Q" + positivePrefix + "\\E";
        negativePrefix = df.getNegativePrefix();
        if (negativePrefix.length() > 0)
            negativePrefix = "\\Q" + negativePrefix + "\\E";
        positiveSuffix = df.getPositiveSuffix();
        if (positiveSuffix.length() > 0)
            positiveSuffix = "\\Q" + positiveSuffix + "\\E";
        negativeSuffix = df.getNegativeSuffix();
        if (negativeSuffix.length() > 0)
            negativeSuffix = "\\Q" + negativeSuffix + "\\E";

        // Force rebuilding and recompilation of locale dependent
        // primitive patterns
        integerPattern = null;
        floatPattern = null;

        return this;
    
public java.util.ScanneruseRadix(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; see localized numbers above.

If the radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, then an IllegalArgumentException is thrown.

param
radix The radix to use when scanning numbers
return
this scanner
throws
IllegalArgumentException if radix is out of range

        if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
            throw new IllegalArgumentException("radix:"+radix);

        if (this.defaultRadix == radix)
            return this;
        this.defaultRadix = radix;
        // Force rebuilding and recompilation of radix dependent patterns
        integerPattern = null;
        return this;
    
private voiduseTypeCache()

        if (closed)
            throw new IllegalStateException("Scanner closed");
        position = hasNextPosition;
        hasNextPattern = null;
        typeCache = null;