java.util

Class Scanner

public class Scanner extends Object implements Iterator<String>

Constructor Summary
Scanner(File source)
Constructs a new Scanner with the given File as source. Scanner
Scanner(File source, String charsetName)
Constructs a new Scanner with the given File as source.
Scanner(InputStream source)
Constructs a new Scanner with the given inputStream.
Scanner(InputStream source, String charsetName)
Constructs a new Scanner with the InputSream and a charsetName.
Scanner(Readable source)
Constructs a new Scanner with a Readable input as source.
Scanner(ReadableByteChannel source)
Constructs a new Scanner with a ReadableByteChannel as source.
Scanner(ReadableByteChannel source, String charsetName)
Constructs a new Scanner with a ReadableByteChannel as source and a given charsetName, which is to be applied on it.
Scanner(String source)
Constructs a new Scanner using the given String as input only.
Method Summary
voidclose()
Closes this Scanner.
Patterndelimiter()
Returns the current delimiter.
StringfindInLine(Pattern pattern)
Tries to find the pattern in the current line.
StringfindInLine(String pattern)
Compiles the given pattern into a Pattern and calls findInLine with the compiled pattern and returns whatever it returns.
StringfindWithinHorizon(Pattern pattern, int horizon)
Trys to match the pattern within the given horizon.
StringfindWithinHorizon(String pattern, int horizon)
Compile the pattern and call Scanner.
booleanhasNext()
Checks if there is any next String using the current delimiter.
booleanhasNext(Pattern pattern)
Searches the pattern in the next subString before the next current delimiter.
booleanhasNext(String pattern)
Compiles the pattern to a Pattern and calls hasNext.
booleanhasNextBigDecimal()
Checks if the string to the next delimiter can be interpreted as a BigDecimal number.
booleanhasNextBigInteger()
Checks if the string to the next delimiter can be interpreted as a BigInteger number.
booleanhasNextBigInteger(int radix)
Checks if the string to the next delimiter can be interpreted as a BigInteger number.
booleanhasNextBoolean()
Checks if the next string could be a boolean.
booleanhasNextByte()
Checks if the string to the next delimiter can be interpreted as a byte number.
booleanhasNextByte(int radix)
Checks if the string to the next delimiter can be interpreted as a byte number with the given radix.
booleanhasNextDouble()
Checks if the string to the next delimiter can be interpreted as a double number.
booleanhasNextFloat()
Checks if the string to the next delimiter can be interpreted as a double number.
booleanhasNextInt()
Checks if the string to the next delimiter can be interpreted as an int number.
booleanhasNextInt(int radix)
Checks if the string to the next delimiter can be interpreted as an int number with the given radix.
booleanhasNextLine()
Checks if there is a current line, which ends at the next line break or the end of the input.
booleanhasNextLong()
Checks if the string to the next delimiter can be interpreted as a long number.
booleanhasNextLong(int radix)
Checks if the string to the next delimiter can be interpreted as a long number with the given radix.
booleanhasNextShort()
Checks if the string to the next delimiter can be interpreted as a short number with the given radix.
booleanhasNextShort(int radix)
Checks if the string to the next delimiter can be interpreted as a short number.
IOExceptionioException()
Returns the last IOException occured.
booleanisUseLocale()
Returns the current value of useLocale.
Localelocale()
Returns the current Locale.
MatchResultmatch()
Returns the last MatchResult found.
Stringnext()
Uses the current delimiter to find the next string in the buffer.
Stringnext(Pattern pattern)
Tries to match the buffer with the given pattern.
Stringnext(String pattern)
Tries to match the buffer with the given pattern.
BigDecimalnextBigDecimal()
Tries to interpret the next string as a BigDecimal value.
BigIntegernextBigInteger()
Tries to interpret the next string as a BigInteger value.
BigIntegernextBigInteger(int radix)
Tries to interpret the next string as a BigInteger value with the given radix.
booleannextBoolean()
Tries to interpret the next string to the delimiter as a boolean value, ignoring case.
bytenextByte()
Tries to interpret the next string as a byte value.
bytenextByte(int radix)
Tries to interpret the next string as a byte value with the given radix.
doublenextDouble()
Tries to interpret the next string as a double value.
floatnextFloat()
Tries to interpret the next string as a double value, and then casts down to float.
intnextInt()
Tries to interpret the next string as an int value.
intnextInt(int radix)
Tries to interpret the next string as an int value with the given radix.
StringnextLine()
Tries to match the system line seperator, and returns the current line.
longnextLong()
Tries to interpret the next string as a long value.
longnextLong(int radix)
Tries to interpret the next string as a long value with the given radix.
shortnextShort()
Tries to interpret the next string as a short value.
shortnextShort(int radix)
Tries to interpret the next string as a short value with the given radix.
intradix()
voidremove()
The remove operation is not supported by this implementation of Iterator.
voidsetUseLocale(boolean useLocale)
Scannerskip(Pattern pattern)
Skips the given pattern.
Scannerskip(String pattern)
Skips a given pattern.
ScanneruseDelimiter(Pattern pattern)
Sets the current pattern to the given parameter, and updates the Matcher with the new pattern.
ScanneruseDelimiter(String pattern)
Sets the current pattern to the given parameter.
ScanneruseLocale(Locale locale)
Sets the current Locale to the given parameter.
ScanneruseRadix(int radix)
Sets the current radix to the current value if the given radix is >= 2 and <= 36 otherwise an IllegalArgumentException is thrown.

Constructor Detail

Scanner

public Scanner(File source)
Constructs a new Scanner with the given File as source. Scanner is called with null as charsetName.

Parameters: source The File to use as source.

Throws: FileNotFoundException If the file is not found an Exception is thrown.

Scanner

public Scanner(File source, String charsetName)
Constructs a new Scanner with the given File as source.
Scanner is called with the given charsetName.

Parameters: source The File to use as source. charsetName Current charset name of the file. If charsetName is null it behaves if it was not set.

Throws: FileNotFoundException If the file is not found an Exception is thrown.

Scanner

public Scanner(InputStream source)
Constructs a new Scanner with the given inputStream.
Scanner is called with null as charsetName.

Parameters: source The InputStream to use as source.

Scanner

public Scanner(InputStream source, String charsetName)
Constructs a new Scanner with the InputSream and a charsetName. Afterwards the Buffer is filled.

Parameters: source The InputStream to use as source. charsetName The charsetName to apply on the source's data.

Scanner

public Scanner(Readable source)
Constructs a new Scanner with a Readable input as source.

Parameters: source The Readable to use as source.

Scanner

public Scanner(ReadableByteChannel source)
Constructs a new Scanner with a ReadableByteChannel as source. Therfore the Scanner is called with null as charsetName.

Parameters: source The ReadableByteChannel to use as source.

Scanner

public Scanner(ReadableByteChannel source, String charsetName)
Constructs a new Scanner with a ReadableByteChannel as source and a given charsetName, which is to be applied on it.
It also initiates the main Buffer.

Parameters: source The ReadableByteChannel to use as source. charsetName The charsetName to be applied on the source.

Scanner

public Scanner(String source)
Constructs a new Scanner using the given String as input only.

Parameters: source The whole String to be used as source.

Method Detail

close

public void close()
Closes this Scanner. If an IOException occurs it is catched and is available under ioException.
After the Scanner is closed, all searches will lead to a IllegalStateException.

delimiter

public Pattern delimiter()
Returns the current delimiter.

Returns: the current delimiter.

findInLine

public String findInLine(Pattern pattern)
Tries to find the pattern in the current line.

Parameters: pattern The pattern which should be searched in the current line of the input.

Returns: If the search was successful, the result or otherwise a NoSuchElementException is thrown.

Throws: NoSuchElementException If the pattern was not found.

findInLine

public String findInLine(String pattern)
Compiles the given pattern into a Pattern and calls findInLine with the compiled pattern and returns whatever it returns.

Parameters: pattern The pattern which should be matched in the input.

Returns: The match in the current line.

Throws: NoSuchElementException If the pattern was not found.

findWithinHorizon

public String findWithinHorizon(Pattern pattern, int horizon)
Trys to match the pattern within the given horizon.

Parameters: pattern Pattern to search. horizon

Returns: The result of the match.

Throws: IllegalArgumentException if the horizon is negative. IllegalStateException if the Scanner is closed.

findWithinHorizon

public String findWithinHorizon(String pattern, int horizon)
Compile the pattern and call Scanner.

Parameters: pattern Pattern to search. horizon

Returns: The result of the match.

Throws: IllegalArgumentException if the horizon is negative. IllegalStateException if the Scanner is closed.

hasNext

public boolean hasNext()
Checks if there is any next String using the current delimiter. Therefore the string must not be null and the length must be greater then 0. If a NoSuchElementException is thrown by the search method, it is catched and false is returned.

Returns: true if there is any result using the current delimiter. This wouldn't lead to a NoSuchElementException.

Throws: IllegalStateException if the Scanner is closed.

hasNext

public boolean hasNext(Pattern pattern)
Searches the pattern in the next subString before the next current delimiter.

Parameters: pattern The pattern to search for.

Returns: true if the pattern is found before the current delimiter.

Throws: IllegalStateException if the Scanner is closed.

hasNext

public boolean hasNext(String pattern)
Compiles the pattern to a Pattern and calls hasNext.

Parameters: pattern The pattern as string to search for.

Returns: true if the pattern is found before the current delimiter.

Throws: IllegalStateException if the Scanner is closed.

See Also: hasNext

hasNextBigDecimal

public boolean hasNextBigDecimal()
Checks if the string to the next delimiter can be interpreted as a BigDecimal number.
BigDecimal numbers are always tryed with radix 10.

Returns: true if the next string is a BigDecimal number.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextBigDecimal

hasNextBigInteger

public boolean hasNextBigInteger()
Checks if the string to the next delimiter can be interpreted as a BigInteger number.
Call Scanner with the current radix.

Returns: true if the next string is a BigInteger number.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextBigInteger

hasNextBigInteger

public boolean hasNextBigInteger(int radix)
Checks if the string to the next delimiter can be interpreted as a BigInteger number.

Parameters: radix The radix to use for this check. The global radix of the Scanner will not be changed.

Returns: true if the next string is a BigInteger number.

Throws: IllegalStateException if the Scanner is closed.

hasNextBoolean

public boolean hasNextBoolean()
Checks if the next string could be a boolean. The method handles the input not case sensitiv, so "true" and "TRUE" and even "tRuE" are true .

Returns: Return true if the next string is a boolean.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextBoolean

hasNextByte

public boolean hasNextByte()
Checks if the string to the next delimiter can be interpreted as a byte number.
Calls Scanner with the current radix.

Returns: true if the next string is a byte number.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextByte

hasNextByte

public boolean hasNextByte(int radix)
Checks if the string to the next delimiter can be interpreted as a byte number with the given radix.
To check, the private method Scanner is called, and if no error occurs the next string could be a byte.

Parameters: radix The radix to use for this check. The global radix of the Scanner will not be changed.

Returns: true if the next string is a byte number.

Throws: IllegalStateException if the Scanner is closed.

See Also: Scanner

hasNextDouble

public boolean hasNextDouble()
Checks if the string to the next delimiter can be interpreted as a double number.
To check, the private method Scanner is called, and if no error occurs the next string could be a double.

Returns: true if the next string is a double number.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextDouble

hasNextFloat

public boolean hasNextFloat()
Checks if the string to the next delimiter can be interpreted as a double number. Because every float is a double this is checked.
To check, the private method Scanner is called, and if no error occurs the next string could be a double.

Returns: true if the next string is a double number.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextFloat

hasNextInt

public boolean hasNextInt()
Checks if the string to the next delimiter can be interpreted as an int number.
To check, the private method Scanner is called, and if no error occurs the next string could be an int.

Returns: true if the next string is an int number.

Throws: IllegalStateException if the Scanner is closed.

See Also: Scanner

hasNextInt

public boolean hasNextInt(int radix)
Checks if the string to the next delimiter can be interpreted as an int number with the given radix.
To check, the private method Scanner is called, and if no error occurs the next string could be an int.

Parameters: radix The radix to use for this check. The global radix of the Scanner will not be changed.

Returns: true if the next string is an int number.

Throws: IllegalStateException if the Scanner is closed.

See Also: Scanner

hasNextLine

public boolean hasNextLine()
Checks if there is a current line, which ends at the next line break or the end of the input.

Returns: true if there is a current line.

Throws: IllegalStateException if the Scanner is closed.

hasNextLong

public boolean hasNextLong()
Checks if the string to the next delimiter can be interpreted as a long number.
To check, the private method Scanner is called, and if no error occurs the next string could be a long.

Returns: true if the next string is a long number.

Throws: IllegalStateException if the Scanner is closed.

See Also: nextLong

hasNextLong

public boolean hasNextLong(int radix)
Checks if the string to the next delimiter can be interpreted as a long number with the given radix.
To check, the private method Scanner is called, and if no error occurs the next string could be a long.

Parameters: radix The radix to use for this check. The global radix of the Scanner will not be changed.

Returns: true if the next string is a long number.

Throws: IllegalStateException if the Scanner is closed.

See Also: Scanner

hasNextShort

public boolean hasNextShort()
Checks if the string to the next delimiter can be interpreted as a short number with the given radix.
To check, the private method Scanner is called, and if no error occurs the next string could be a short.

Returns: true if the next string is a short number.

Throws: IllegalStateException if the Scanner is closed.

See Also: Scanner

hasNextShort

public boolean hasNextShort(int radix)
Checks if the string to the next delimiter can be interpreted as a short number.
To check, the private method Scanner is called, and if no error occurs the next string could be a short.

Parameters: radix The radix to use for this check. The global radix of the Scanner will not be changed.

Returns: true if the next string is a short number.

Throws: IllegalStateException if the Scanner is closed.

See Also: Scanner

ioException

public IOException ioException()
Returns the last IOException occured.

Returns: Returns the last IOException.

isUseLocale

public boolean isUseLocale()
Returns the current value of useLocale. This is used to tell the Scanner if it should use the Locale format or just handle numbers of the default format.

Returns: the useLoclae.

See Also: Scanner

locale

public Locale locale()
Returns the current Locale. It is initialized with getDefault.

Returns: Returns the current Locale.

See Also: useLocale

match

public MatchResult match()
Returns the last MatchResult found. This is updated after every successfully search.

Returns: Returns the last MatchResult found.

next

public String next()
Uses the current delimiter to find the next string in the buffer. If a string is found the current position is set after the delimiter, otherwise a NoSuchElementException is thrown. A successful match sets the matchResult.

Returns: Returns the next string of the buffer.

Throws: NoSuchElementException If no element was found an exception is thrown. IllegalStateException If the Scanner is closed.

See Also: match

next

public String next(Pattern pattern)
Tries to match the buffer with the given pattern. The current delimiter will not be changed.

Parameters: pattern The pattern to match.

Returns: Returns the next string matching the pattern.

Throws: NoSuchElementException If no element was found an exception is thrown. IllegalStateException If the Scanner is closed.

next

public String next(String pattern)
Tries to match the buffer with the given pattern. The current delimiter will not be changed. Calls the next with the compiled pattern.

Parameters: pattern The pattern to match.

Returns: Returns the next string matching the pattern.

Throws: NoSuchElementException If no element was found an exception is thrown. IllegalStateException If the Scanner is closed.

See Also: next

nextBigDecimal

public BigDecimal nextBigDecimal()
Tries to interpret the next string as a BigDecimal value.

Returns: Returns the BigDecimal value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a BigDecimal. IllegalStateException If the Scanner is closed.

nextBigInteger

public BigInteger nextBigInteger()
Tries to interpret the next string as a BigInteger value. Call Scanner with the current radix as parameter, and return the value.

Returns: Returns the BigInteger value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a BigInteger. IllegalStateException If the Scanner is closed.

See Also: Scanner

nextBigInteger

public BigInteger nextBigInteger(int radix)
Tries to interpret the next string as a BigInteger value with the given radix.

Parameters: radix The radix to be used for this BigInteger. The current radix of the Scanner is not changed.

Returns: Returns the BigInteger value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a BigInteger. IllegalStateException If the Scanner is closed.

nextBoolean

public boolean nextBoolean()
Tries to interpret the next string to the delimiter as a boolean value, ignoring case.

Returns: Returns the boolean value of the next matching string or throws an exception.

Throws: NoSuchElementException If no string is found or the string is not a boolean. IllegalStateException If the Scanner is closed.

nextByte

public byte nextByte()
Tries to interpret the next string as a byte value. Call Scanner with the current radix as parameter, and return the value.

Returns: Returns the byte value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a byte IllegalStateException If the Scanner is closed.

See Also: Scanner

nextByte

public byte nextByte(int radix)
Tries to interpret the next string as a byte value with the given radix.

Parameters: radix The radix to be used for this byte. The current radix of the Scanner is not changed.

Returns: Returns the byte value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a byte. IllegalStateException If the Scanner is closed.

nextDouble

public double nextDouble()
Tries to interpret the next string as a double value.

Returns: Returns the int value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a double. IllegalStateException If the Scanner is closed.

nextFloat

public float nextFloat()
Tries to interpret the next string as a double value, and then casts down to float.

Returns: Returns the int value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a double. IllegalStateException If the Scanner is closed.

nextInt

public int nextInt()
Tries to interpret the next string as an int value. Calls Scanner with the current radix as parameter, and return the value.

Returns: Returns the int value of the next string.

Throws: NoSuchElementException If no string is found or the string is not an int. IllegalStateException If the Scanner is closed.

See Also: Scanner

nextInt

public int nextInt(int radix)
Tries to interpret the next string as an int value with the given radix.

Parameters: radix The radix to be used for this int. The current radix of the Scanner is not changed

Returns: Returns the int value of the next string.

Throws: NoSuchElementException If no string is found or the string is not an int. IllegalStateException If the Scanner is closed.

nextLine

public String nextLine()
Tries to match the system line seperator, and returns the current line.

Returns: Returns the current line.

Throws: NoSuchElementException If the current delimiter is not found. IllegalStateException If the Scanner is closed.

nextLong

public long nextLong()
Tries to interpret the next string as a long value. Calls Scanner with the current radix as parameter, and return the value.

Returns: Returns the long value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a long. IllegalStateException If the Scanner is closed.

See Also: Scanner

nextLong

public long nextLong(int radix)
Tries to interpret the next string as a long value with the given radix.

Parameters: radix The radix to be used for this long. The current radix of the Scanner is not changed

Returns: Returns the long value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a long. IllegalStateException If the Scanner is closed.

nextShort

public short nextShort()
Tries to interpret the next string as a short value. Calls Scanner with the current radix as parameter, and return the value.

Returns: Returns the short value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a short.

See Also: Scanner

nextShort

public short nextShort(int radix)
Tries to interpret the next string as a short value with the given radix.

Parameters: radix The radix to be used for this short. The current radix of the Scanner is not changed.

Returns: Returns the short value of the next string.

Throws: NoSuchElementException If no string is found or the string is not a short.

radix

public int radix()

Returns: Returns the current radix.

remove

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

setUseLocale

public void setUseLocale(boolean useLocale)

Parameters: useLocale the useLocale to set.

skip

public Scanner skip(Pattern pattern)
Skips the given pattern. Sets skipped true.

Parameters: pattern Pattern which should be skipped.

Returns: this with the skipped buffer.

Throws: NoSuchElementException If the Pattern is not found.

skip

public Scanner skip(String pattern)
Skips a given pattern. Calls skip with the compiled pattern.

Parameters: pattern Pattern which should be skipped.

Returns: this with the skipped buffer.

See Also: skip

useDelimiter

public Scanner useDelimiter(Pattern pattern)
Sets the current pattern to the given parameter, and updates the Matcher with the new pattern.

Parameters: pattern The new pattern to use.

Returns: Returns the Scanner (this) with the new pattern.

useDelimiter

public Scanner useDelimiter(String pattern)
Sets the current pattern to the given parameter. Compiles the pattern and calls useDelimiter

Parameters: pattern The new pattern to use.

Returns: Returns the Scanner (this) with the new pattern.

See Also: useDelimiter

useLocale

public Scanner useLocale(Locale locale)
Sets the current Locale to the given parameter. Formats and Symbols are also set using the new Locale.

Parameters: locale The new Locale to use. If it is null nothing happens.

Returns: Returns the Scanner (this) with the new Locale.

useRadix

public Scanner useRadix(int radix)
Sets the current radix to the current value if the given radix is >= 2 and <= 36 otherwise an IllegalArgumentException is thrown.

Parameters: radix the new radix to use as default.

Returns: this with the new radix value.

Throws: IllegalArgumentException When the given radix is out of bounds.