public class ScanfReader extends Reader
There are scan methods to read float, double, long, int, char, char[], and String. The methods take as an argument either a format string, a pre-allocated ScanfFormat object which is created from a format string, or no argument (implying a default format). The format string is modeled after that accepted by the C scanf() methodName, and is described in the documentation for the class ScanfFormat.
Because Java does not permit variable-length argument lists, only one primitive type may be returned per method, and the format used may contain only one conversion specification (which must be appropriate to the type being scanned).
Input errors in the underlying Reader result in a java.io.IOException being thrown, while a java.io.EOFException is thrown if the end of input is reached before the scan can complete successfully. If the input does not match the specified format, then a ScanfMatchException is thrown. In the event of a match error, 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. Finally, an invalid format string (or ScanfFormat object) will trigger an InvalidArgumentException.
The class keeps track of the current line number (accessible with the methods getLineNumber and setLineNumber), as well as the number of characters which have been consumed (accesible with the methods 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 the getLookAheadChar method. The look-ahead character can be cleared using the clearLookAheadChar method.
Constructor and Description |
---|
ScanfReader(Reader in)
Create a new ScanfReader from the given reader.
|
Modifier and Type | Method and Description |
---|---|
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 |
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.
|
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(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(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(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(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(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(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(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(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(String s)
Scan and return an octal (long) integer.
|
String |
scanString()
Scan and return a String, using the default format string "%s".
|
String |
scanString(ScanfFormat fmt)
Scan and return a String, using a pre-allocated ScanfFormat object.
|
String |
scanString(String s)
Scan and return a String.
|
void |
setCharNumber(int n)
Sets the current character number.
|
void |
setLineNumber(int n)
Sets the current line number.
|
boolean |
useCstandard()
White spaces are skipped at the beginning of a line if flag is true otherwise spaces are counted as
valid characters.
|
void |
useCstandard(boolean flag)
White spaces are skipped at the beginning of a line if flag is true otherwise spaces are
counted as valid characters.
|
public ScanfReader(Reader in)
reader
- Underlying Readerpublic void clearLookAheadChar()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
IOException
- An I/O error occurredpublic int getCharNumber()
setCharNumber(int)
public int getLineNumber()
setLineNumber(int)
public int getLookAheadChar()
public boolean lookAheadCharValid()
public int read(char[] cbuf, int off, int len) throws IOException
read
in class Reader
cbuf
- Buffer to write characters intooff
- Offset to start writing atlen
- Number of characters to readIOException
- An I/O error occurredpublic char scanChar() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanChar(String)
public char scanChar(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public char scanChar(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanChar(String)
public char[] scanChars(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public char[] scanChars(int n) throws IOException, ScanfMatchException, IllegalArgumentException
n
- Number of characters to readIllegalArgumentException
- n not a positive numberScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanChars(String)
public char[] scanChars(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanChars(String)
public long scanDec() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDec(String)
public long scanDec(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public long scanDec(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDec(String)
public double scanDouble() throws IOException, ScanfMatchException
ScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDouble(String)
public double scanDouble(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public double scanDouble(ScanfFormat fmt) throws IOException, ScanfMatchException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDouble(String)
public float scanFloat() throws IOException, ScanfMatchException
ScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDouble(String)
public float scanFloat(String s) throws IOException, ScanfMatchException, IllegalArgumentException
s
- Format stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDouble(String)
public float scanFloat(ScanfFormat fmt) throws IOException, ScanfMatchException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDouble(String)
public long scanHex() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanHex(String)
public long scanHex(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public long scanHex(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanHex(String)
public int scanInt() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanInt(String)
public int scanInt(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanDec(String)
,
scanOct(String)
,
scanHex(String)
public int scanInt(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanInt(String)
public long scanLong() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanInt(String)
public long scanLong(String s) throws IOException, ScanfMatchException, IllegalArgumentException
s
- Format stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorscanInt(String)
public long scanLong(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorscanInt(String)
public long scanOct() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanOct(String)
public long scanOct(String s) throws IOException, ScanfMatchException, 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 stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public long scanOct(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanOct(String)
public String scanString() throws IOException, ScanfMatchException
s
- Format stringScanfMatchException
- Input did not match formatEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanString(String)
public String scanString(String s) throws IOException, ScanfMatchException, IllegalArgumentException
The format string s must have the form described by the documentation for the class ScanfFormat , and must contain the conversion character 's'. The string returned corresponds to the next non-white-space sequence of characters found in the input, with preceding white space skipped.
s
- Format stringScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
public String scanString(ScanfFormat fmt) throws IOException, IllegalArgumentException
fmt
- Format objectScanfMatchException
- Input did not match formatIllegalArgumentException
- Error in format specificationEOFException
- End of fileIOException
- Other input errorScanfFormat
,
scanString(String)
public void setCharNumber(int n)
n
- New character numbergetCharNumber()
public void setLineNumber(int n)
n
- New line numbersetLineNumber(int)
public boolean useCstandard()
public void useCstandard(boolean flag)
Copyright © 2018. All rights reserved.