|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.io.Reader | +--cformat.ScanfReader
A Reader which implements C scanf
functionality.
It allows an application to read various primitive types
from the underlying stream using scan
methods
that implement scanf
type input formatting.
There are scan
methods to read float, double,
long, int, char, char[], and Strings, and each can accept a format
string with the same syntax as that accepted by the C scanf()
function (the exact syntax is described in the
documentation for ScanfFormat
).
Because Java does not permit variable-length argument lists, only one conversion can be performed per method call. As an example, suppose we wish to parse the input
00010987JohnLloyd 0032,09999.99with the equivalent of
scanf ("%10d%4s%10s%d,%f", &i, name1, name2, &j, &price);This can be done as follows:
ScanfReader scanner = new ScanfReader (reader); int i = scanner.scanInt ("%10d"); String name1 = scanner.scanString ("%4s"); String name2 = scanner.scanString ("%10s"); int j = scanner.scanInt ("%d"); double price = scanner.scanDouble (",%f");
The scan methods can also be called without an argument,
in which case a default format string is used. One can also
pre-parse the format string into an object of type
ScanfFormat
,
which is more efficient when a lot of input is being read:
ScanfFormat fmt = new ScanfFormat ("%f"); for (int i=0; i<100000; i++){ { buf[i] = scanner.scanDouble (fmt); }
If the input does not match the specified format, then a
ScanfMatchException
is thrown. A
java.io.EOFException
is thrown if the end of input is
reached before the scan can complete successfully, and errors in
the underlying input will throw a
java.io.IOException
. Finally, an invalid format
string (or ScanfFormat
object) will trigger an
InvalidArgumentException
.
In the case of a ScanfMatchException
,
scanning stops at the first character from which it can be
determined that the match will fail. This character is remembered by
the stream (see the discussion of the look-ahead character, below)
and will be the first character seen by the next scan
or
read
method which is called.
The class keeps track of the current line number (accessible with
getLineNumber
and
setLineNumber
),
as well as the number of characters which have been consumed
(accesible with getCharNumber
and setCharNumber
).
The class usually keeps one character of look-ahead which has been
read from the underlying reader but not yet consumed by any scan
method. If the underlying reader is used later in some other capacity,
this look-ahead character may have to be taken into account. If a
look-ahead character is actually being stored, the
lookAheadCharValid
method
will return true
,
and the look-ahead character itself can then be obtained using
getLookAheadChar
. The look-ahead character can
be cleared using clearLookAheadChar
.
Constructor Summary | |
ScanfReader(java.io.Reader in)
Create a new ScanfReader from the given reader. |
Method Summary | |
void |
clearLookAheadChar()
Clears the look-ahead character. |
void |
close()
Closes the stream. |
int |
getCharNumber()
Gets the current character number (equal to the number of characters that have been consumed by the stream). |
int |
getCommentChar()
Gets the comment character. |
int |
getLineNumber()
Gets the current line number. |
int |
getLookAheadChar()
Returns the look-ahead character. |
boolean |
lookAheadCharValid()
Returns whether or not a look-ahead character is currently begin stored. |
int |
read(char[] cbuf,
int off,
int len)
Reads characters into a portion of a character array. |
boolean |
scanBoolean()
Scan and return a boolean , using the default format
string "%b". |
boolean |
scanBoolean(ScanfFormat fmt)
Scan and return a boolean , using a pre-allocated
ScanfFormat object. |
boolean |
scanBoolean(java.lang.String s)
Scan and return a boolean . |
char |
scanChar()
Scan and return a single character, using the default format string "%c". |
char |
scanChar(ScanfFormat fmt)
Scan and return a single character, using a pre-allocated ScanfFormat object. |
char |
scanChar(java.lang.String s)
Scan and return a single character. |
char[] |
scanChars(int n)
Scan and return a character array, using the default format string "%c", with the field width (number of characters to read) supplanted by the argument n . |
char[] |
scanChars(ScanfFormat fmt)
Scan and return a character array, using a pre-allocated ScanfFormat object. |
char[] |
scanChars(java.lang.String s)
Scan and return a character array, whose size is determined by the field width specified in the format string (with a default width of 1 being assumed if no width is specified). |
long |
scanDec()
Scan and return a signed decimal (long) integer, using the default format string "%d". |
long |
scanDec(ScanfFormat fmt)
Scan and return a signed decimal (long) integer, using a pre-allocated ScanfFormat object. |
long |
scanDec(java.lang.String s)
Scan and return a signed decimal (long) integer. |
double |
scanDouble()
Scan and return a double, using the default format string "%f". |
double |
scanDouble(ScanfFormat fmt)
Scan and return a double, using a pre-allocated ScanfFormat object. |
double |
scanDouble(java.lang.String s)
Scan and return a double. |
float |
scanFloat()
Scan and return a float, using the default format string "%f". |
float |
scanFloat(ScanfFormat fmt)
Scan and return a float, using a pre-allocated ScanfFormat object. |
float |
scanFloat(java.lang.String s)
Scan and return a float. |
long |
scanHex()
Scan and return a hex (long) integer, using the default format string "%x". |
long |
scanHex(ScanfFormat fmt)
Scan and return a hex (long) integer, using a pre-allocated ScanfFormat object. |
long |
scanHex(java.lang.String s)
Scan and return a hex (long) integer. |
int |
scanInt()
Scan and return a signed integer, using the default format string "%i". |
int |
scanInt(ScanfFormat fmt)
Scan and return a signed integer, using a pre-allocated ScanfFormat object. |
int |
scanInt(java.lang.String s)
Scan and return a signed integer. |
long |
scanLong()
Scan and return a signed (long) integer, using the default format string "%i". |
long |
scanLong(ScanfFormat fmt)
Scan and return a signed (long) integer, using a pre-allocated ScanfFormat object. |
long |
scanLong(java.lang.String s)
Scan and return a signed (long) integer. |
long |
scanOct()
Scan and return an octal (long) integer, using the default format string "%o". |
long |
scanOct(ScanfFormat fmt)
Scan and return an octal (long) integer, using a pre-allocated ScanfFormat object. |
long |
scanOct(java.lang.String s)
Scan and return an octal (long) integer. |
java.lang.String |
scanString()
Scan and return a String , using the default format
string "%s". |
java.lang.String |
scanString(ScanfFormat fmt)
Scan and return a String , using a pre-allocated
ScanfFormat object. |
java.lang.String |
scanString(java.lang.String s)
Scan and return a String . |
void |
setCharNumber(int n)
Sets the current character number. |
void |
setCommentChar(int c)
Sets the comment character. |
void |
setLineNumber(int n)
Sets the current line number. |
Methods inherited from class java.io.Reader |
mark, markSupported, read, read, ready, reset, skip |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ScanfReader(java.io.Reader in)
Method Detail |
public void close() throws java.io.IOException
close
in class java.io.Reader
java.io.IOException
- An I/O error occurredpublic int read(char[] cbuf, int off, int len) throws java.io.IOException
read
in class java.io.Reader
cbuf
- Buffer to write characters intooff
- Offset to start writing atlen
- Number of characters to read
java.io.IOException
- An I/O error occurredpublic double scanDouble(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion character 'f'. The number itself
may consist of (a) an optional sign ('+' or '-'), (b) a sequence
of decimal digits, with an optional decimal point, (c) an
optional exponent ('e' or 'E'), which must by followed by an
optionally signed sequence of decimal digits. White space
immediately before the number is skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public double scanDouble() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDouble(String)
public double scanDouble(ScanfFormat fmt) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDouble(String)
public float scanFloat(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
s
takes the same form as that described in the documentation
for scanDouble(String)
.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDouble(String)
public float scanFloat() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDouble(String)
public float scanFloat(ScanfFormat fmt) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDouble(String)
public java.lang.String scanString(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
String
.
The format string s
must have the form
described by the documentation for the class
ScanfFormat
, and must contain either the
conversion character 's', '[', 'q', 'S', or no conversion
character. If the conversion character is 's', then the
returned string corresponds to the next non-white-space
sequence of characters found in the input, with preceding
white space skipped. If the conversion character is '[',
then the returned string corresponds to the next sequence
of characters which match those specified between the '['
and the closing ']' (see the documentation for
ScanfFormat
). If the conversion character is
'q', then the returned string corresponds to the next
sequence of characters delimited by double quotes, with
preceding white space skipped. If the conversion
character is 'S', then the returned string corresponds to
the next sequence of characters delimited by either
white-space or double quotes, with preceding white space
skipped. If there is no conversion character, then the
input must match the format string, except that white
space in the format may be matched by any amount of white
space, including none, in the input.
s
- Format string
String
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public java.lang.String scanString() throws java.io.IOException, ScanfMatchException
String
, using the default format
string "%s".
String
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanString(String)
public java.lang.String scanString(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
String
, using a pre-allocated
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
String
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanString(String)
public boolean scanBoolean(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
boolean
.
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion character 'b'. White space
before the conversion is skipped. The function returns true
if the input matches the string "true"
, and false
if the input matches the string "false"
. Other
inputs will cause a ScanfMatchException to be thrown. Any
field width specification in the format is ignored.
s
- Format string
boolean
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public boolean scanBoolean() throws java.io.IOException, ScanfMatchException
boolean
, using the default format
string "%b".
boolean
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanBoolean(String)
public boolean scanBoolean(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
boolean
, using a pre-allocated
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
boolean
value
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanBoolean(String)
public char[] scanChars(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion characters 'c' or '['. If the
conversion character is '[', then each character scanned must
match the sequence specified between the '[' and the closing
']' (see the documentation for ScanfFormat
).
White space preceding the character sequence is not skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public char[] scanChars(int n) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
n
.
n
- Number of characters to read
java.lang.IllegalArgumentException
- n
not a positive
number
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanChars(String)
public char[] scanChars(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanChars(String)
public char scanChar(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion character 'c' or '['. If the
conversion character is '[', then each character scanned must
match the sequence specified between the '[' and the closing
']' (see the documentation for ScanfFormat
).
White space preceding the character is not skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public char scanChar() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanChar(String)
public char scanChar(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanChar(String)
public long scanHex(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion character 'x'. The integer itself
must be formed from the characters [0-9a-fA-F], and white space
which immediately precedes it is skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public long scanHex() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanHex(String)
public long scanHex(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanHex(String)
public long scanOct(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion character 'o'. The integer itself
must be composed of the digits [0-7], and white space which
immediately precedes it is skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public long scanOct() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanOct(String)
public long scanOct(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanOct(String)
public long scanDec(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain the conversion character 'd'.
The integer itself must consist of an optional sign
('+' or '-') followed by a sequence of digits. White space
preceding the number is skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
public long scanDec() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDec(String)
public long scanDec(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDec(String)
public int scanInt(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
The format string s
must have the form described
by the documentation for the class ScanfFormat
,
and must contain one of the conversion characters "doxi".
Specifying the conversion characters 'd', 'o', or 'x' is
equivalent to calling (int versions of) scanDec
,
scanOct
, and scanHex
, respectively.
If the conversion character is 'i', then after an optional
sign ('+' or '-'), if the number begins with an
0x
, then it is scanned as a hex number;
if it begins with an 0
, then it is scanned as an
octal number, and otherwise it is scanned as a decimal number.
White space preceding the number is skipped.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanDec(String)
,
scanOct(String)
,
scanHex(String)
public int scanInt() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanInt(String)
public int scanInt(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
This saves the overhead of parsing the format from a string.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanInt(String)
public long scanLong(java.lang.String s) throws java.io.IOException, ScanfMatchException, java.lang.IllegalArgumentException
scanInt(String)
.
s
- Format string
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorscanInt(String)
public long scanLong() throws java.io.IOException, ScanfMatchException
ScanfMatchException
- Input did not match format
java.io.EOFException
- End of file
java.io.IOException
- Other input errorScanfFormat
,
scanInt(String)
public long scanLong(ScanfFormat fmt) throws java.io.IOException, java.lang.IllegalArgumentException
ScanfFormat
object.
fmt
- Format object
ScanfMatchException
- Input did not match format
java.lang.IllegalArgumentException
- Error in format specification
java.io.EOFException
- End of file
java.io.IOException
- Other input errorscanInt(String)
public int getCharNumber()
setCharNumber(int)
public void setCharNumber(int n)
n
- New character numbergetCharNumber()
public int getLineNumber()
setLineNumber(int)
public void setLineNumber(int n)
n
- New line numbersetLineNumber(int)
public boolean lookAheadCharValid()
public int getLookAheadChar()
public void clearLookAheadChar()
public int getCommentChar()
setCommentChar(int)
public void setCommentChar(int c)
%
or #
are used, this is unlikely ot be of
consequence. A value of -1 disables
the comment character.
c
- New comment charactergetCommentChar()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |