Package util
Class Scanner
java.lang.Object
util.Scanner
Class for simple, fairly generic scanners (tokenizers).
An input stream or a string is broken into tokens, which
are returned one by one. The tokens that are recognized
a fairly common set that fits a variety of applications.
- Since:
- 2004.05.10
-
Field Summary
FieldsModifier and TypeFieldDescriptionint
current line number (determined by counting newline characters)static final int
token: two character comparison operatorstatic final int
token: dash '--'static final int
token: end of filestatic final int
token: identifier or stringstatic final int
token: left arrow '<-'static final int
token: number (floating point)static final int
token: right arrow '->'int
current token typecurrent token value -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the scanner by closing the underlying reader.static final String
Format a string so that it can be read as a token.void
getChar
(char c) Get the next token and compare it to an expected character.void
getID()
Get the next token and check whether it is an identifier.void
Get the next token and compare it to an expected identifier.void
Get the next token and check whether it is an number.lno()
Get a string stating the current line number.static void
Main function for testing basic functionality.int
Get the next token of the input stream or the string.void
nlToken
(boolean flag) Set whether the newline character should be treated as a token or not.void
pushBack()
Push back the last token into the input.void
Push back the last token into the input.
-
Field Details
-
T_EOF
public static final int T_EOFtoken: end of file- See Also:
-
T_NUM
public static final int T_NUMtoken: number (floating point)- See Also:
-
T_ID
public static final int T_IDtoken: identifier or string- See Also:
-
T_RGT
public static final int T_RGTtoken: right arrow '->'- See Also:
-
T_LFT
public static final int T_LFTtoken: left arrow '<-'- See Also:
-
T_DASH
public static final int T_DASHtoken: dash '--'- See Also:
-
T_CMP
public static final int T_CMPtoken: two character comparison operator- See Also:
-
ttype
public int ttypecurrent token type -
value
current token value -
line
public int linecurrent line number (determined by counting newline characters)
-
-
Constructor Details
-
Scanner
Create a scanner working on a reader.- Parameters:
reader
- the reader to work on- Since:
- 2004.05.10 (Christian Borgelt)
-
Scanner
Create a scanner working on a string.- Parameters:
string
- the string to scan- Since:
- 2004.05.10 (Christian Borgelt)
-
Scanner
Create a scanner working on an input stream.- Parameters:
stream
- the input stream to scan- Since:
- 2004.05.10 (Christian Borgelt)
-
-
Method Details
-
lno
Get a string stating the current line number. Useful for error reporting.- Returns:
- a string stating the current line number in the format "(line xxx)"
- Since:
- 2004.05.20 (Christian Borgelt)
-
nlToken
public void nlToken(boolean flag) Set whether the newline character should be treated as a token or not. By default it is treated as a whitespace character.- Parameters:
flag
- iftrue
, the newline character is treated as a token, otherwise as whitespace.- Since:
- 2005.03.01 (Christian Borgelt)
-
nextToken
Get the next token of the input stream or the string.- Returns:
- the code of the next token
- Throws:
IOException
- if an I/O error occurs or the scanner detects an error in the format of the stream- Since:
- 2004.05.10 (Christian Borgelt)
-
ungetToken
public void ungetToken()Push back the last token into the input. Only the last token can be pushed back into the input; two consecutive calls have the same effect as a single call.- Since:
- 2004.05.10 (Christian Borgelt)
-
pushBack
public void pushBack()Push back the last token into the input. Only the last token can be pushed back into the input; two consecutive calls have the same effect as a single call.- Since:
- 2004.05.10 (Christian Borgelt)
-
getChar
Get the next token and compare it to an expected character.- Parameters:
c
- the expected character- Throws:
IOException
- if the expected character is not found- Since:
- 2005.02.21 (Christian Borgelt)
-
getID
Get the next token and compare it to an expected identifier.If
null
is passed for the expected identifier, it is only checked whether an identifier follows, while the value of this identifier is ignored.- Parameters:
id
- the expected identifier- Throws:
IOException
- if the expected identifier is not found- Since:
- 2005.02.21 (Christian Borgelt)
-
getID
Get the next token and check whether it is an identifier.- Throws:
IOException
- if the next token is not an identifier- Since:
- 2005.02.21 (Christian Borgelt)
-
getNumber
Get the next token and check whether it is an number.- Throws:
IOException
- if the next token is not a number- Since:
- 2005.02.21 (Christian Borgelt)
-
close
Close the scanner by closing the underlying reader.- Throws:
IOException
- if an I/O error occurs- Since:
- 2007.02.08 (Christian Borgelt)
-
format
Format a string so that it can be read as a token.- Parameters:
s
- the string to formatquotes
- whether to add quotes- Returns:
- the formatted string
- Since:
- 2007.02.05 (Christian Borgelt)
-
main
Main function for testing basic functionality.- Parameters:
args
- the command line argument- Since:
- 2004.05.10 (Christian Borgelt)
-