Class ACMIO
java.lang.Object
|
+----ACMIO
- public class ACMIO
- extends Object
The ACMIO class has methods for reading some primitive data values
from the keyboard and text files, and it has methods to set fieldwidth
and precision in formatting Strings for output. The class was designed
for use in the ACM Mid-Central Regional Competition, November 6, 1999.
See the sample program ACMIOTest.java and sample input file t.txt.
Input method overview
There is a constructor that takes a filename as parameter. For example
ACMIO fileIn = new ACMIO("problem1.in");
You may instantiate multiple file readers. There is only one keyboard,
so a static method returns the only keyboard reader. For example:
ACMIO stdin = ACMIO.getStdin();
IOExceptions and input in illegal formats cause the program to terminate
with an error message to System.err. The illegal input formats are caused
by incorrect numeric format or end-of-file where a token was expected.
Most reading methods skip initial whitespace, but there are two methods
that do not:
getChar() reads the next character, and
getLine() reads a whole line.
Methods with names in the form typeRead skip whitespace and then read an
int, long, double, char or String. Examples are
intRead() and stringRead().
Note all start with a lowercase letter. Method stringRead() reads
the longest String with no whitespace. There are also methods with "ln"
added to the end: intReadln(), longReadln(), doubleReadln(),
charReadln() and stringReadln(), which all skip the rest
of the line.
Note the distinction between that getLine() and
stringReadln().
Both end up after the end of a line, but getLine() returns the
rest of the current line, and stringReadln()skips all whitespace,
possibly through several empty lines, and skips to the end of the line
after the first sequence of non-whitespace characters it finds.
Boolean methods that test the state are isEOLN() and
isEOF(), which read ahead to see if the end of a line or the
end of the file is next.
Void input skipping methods are skipBlanks(),
skipWhitespace(), and skipLine().
String formatting method overview:
To be comparable with output formatting in Pascal and C and C++, there
are a group of overloaded static methods to specify the precision of a
double and the minimum fieldwidth and left or right justification for any
String.
String format(long l, int minimumWidth)
String format(Object o, int minimumWidth)
String format(double d, int minimumWidth)
String format(double d, int minimumWidth, int fractionDigits)
where a positive minimumWidth means right-justify and negative
means left-justify, and both are in a field of at least |minimumWidth|
columns.
Explicit overloadings for the justification are only given for numbers
and for objects, but anything can be converted to a String Object by concatenating
it with the empty String: "" + (anything). Examples:
format(123.45678, 7, 2) returns " 123.46", with one
blank on the left.
format(123.45678, -7, 2) returns "123.46 ", with
one blank on the right.
format(-1234, 8) returns " -1234", with
three blanks on the left.
format("abc", -5) returns "abc ", with two
blanks on the right.
- Version:
- Sept 16, 1999
- Author:
- Andrew Harrington (anh@cs.luc.edu)
-
ACMIO(String)
- Construct an ACMIO to read from a file.
-
charRead()
- Skip whitespace and read a character.
-
charReadln()
- Skip whitespace, read a character, and skip the rest of the line.
-
doubleRead()
- Read a
double number.
-
doubleReadln()
- Read a double value from a complete line.
-
format(double, int)
- Create a left or right justified String from a double value.
-
format(double, int, int)
- Format a double as a String with a specified format.
-
format(long, int)
- Create a left or right justified String from a long value.
-
format(Object, int)
- Create a left or right justified String from an
Object .
-
getChar()
- Read a character.
-
getLine()
- Read until the end of the current line.
-
getStdin()
- Returns the
ACMIO object that reads from System.in.
-
intRead()
- Read an int value.
-
intReadln()
- Read an int value from a complete line.
-
isEOF()
- Determine whether the last character of the file has been read.
-
isEOLN()
- Determine whether the next input character is an end of line or end of file.
-
longRead()
- Read a long value.
-
longReadln()
- Read a long value from a complete line.
-
skipBlanks()
- Read and discard blanks.
-
skipLine()
- Read and discard the rest of the current input line.
-
skipWhitespace()
- Read and discard whitespace: blanks, tabs, and newlines.
-
stringRead()
- Read the next whitespace-delimited
String.
-
stringReadln()
- Read the next whitespace-delimited
String,
and skip the rest of the line it is on.
ACMIO
public ACMIO(String filename)
- Construct an ACMIO to read from a file.
- Parameters:
-
filename - file to read from.
- Abort
if the file cannot be opened to read.
getStdin
public static ACMIO getStdin()
- Returns the
ACMIO object that reads from System.in.
- Returns:
-
ACMIO object that reads from System.in
skipWhitespace
public void skipWhitespace()
- Read and discard whitespace: blanks, tabs, and newlines.
skipBlanks
public void skipBlanks()
- Read and discard blanks.
skipLine
public void skipLine()
- Read and discard the rest of the current input line.
getLine
public String getLine()
- Read until the end of the current line.
- Returns:
- a
String with all characters other that '\r' or '\n'
until the end of the line, or end-of-file if that comes first.
getChar
public char getChar()
- Read a character.
- Returns:
- a character read
- Abort
if tried to read at end-of-file.
charRead
public char charRead()
- Skip whitespace and read a character.
- Returns:
- a character read that is not whitespace
- Abort
if there was only whitespace until the end-of-file.
charReadln
public char charReadln()
- Skip whitespace, read a character, and skip the rest of the line.
- Returns:
- a character read that is not whitespace
- Abort
if there was only whitespace until the end-of-file.
doubleRead
public double doubleRead()
- Read a
double number.
- Returns:
- a double number that's been read
- Parsing details: Skip whitespace and then read
an optional sign, 0 or more digits, an optional '.'
and 0 or more digits, and an optional exponent
including an 'e' or 'E', optional sign, and digits.
The first character not fitting this pattern is left in the input stream.
The extracted string is converted using
Double.valueOf.
- Abort
if the conversion to a double causes an exception.
doubleReadln
public double doubleReadln()
- Read a double value from a complete line.
- Returns:
- a double value that's been read
- Note:
-
This method is identical to
doubleRead() with
skipLine() called just before returning.
intRead
public int intRead()
- Read an int value.
- Returns:
- an int that has been read
- Parsing details: Skip whitespace and then read
an optional sign and digits.
The first character not fitting this pattern is left in the input stream.
The extracted string is converted using
Integer.parseInt.
- Abort
if the conversion to an int causes an exception.
intReadln
public int intReadln()
- Read an int value from a complete line.
- Returns:
- an int that has been read
- Note:
-
This method is identical to
intRead() with
skipLine() called just before returning.
longRead
public long longRead()
- Read a long value.
- Returns:
- a long value that has been read
- Parsing details: Skip whitespace and then read
an optional sign and digits.
The first character not fitting this pattern is left in the input stream.
The extracted string is converted using
Long.parseInt.
- Abort
if the conversion to a long causes an exception.
longReadln
public long longReadln()
- Read a long value from a complete line.
- Returns:
- a long value that has been read
- Note:
-
This method is identical to
longRead() with
skipLine() called just before returning.
stringRead
public String stringRead()
- Read the next whitespace-delimited
String.
- Returns:
- a
String of non-whitespace characters read
- Abort
if there are no non-whitespace characters until end-of-file.
stringReadln
public String stringReadln()
- Read the next whitespace-delimited
String,
and skip the rest of the line it is on.
- Returns:
- a
String of non-whitespace characters read
- Abort
if there are no non-whitespace characters until end-of-file.
isEOF
public boolean isEOF()
- Determine whether the last character of the file has been read.
- Returns:
- true if all characters up to but not necessaily including EOF have been read.
isEOLN
public boolean isEOLN()
- Determine whether the next input character is an end of line or end of file.
- Returns:
- true if the next input character is '\n' or '\r',
or if
isEOF()
format
public static String format(double d,
int minimumWidth,
int fractionDigits)
- Format a double as a String with a specified format.
- Parameters:
-
d
- the number to be printed
-
minimumWidth
- |minimumWidth| is the minimum number of characters in the entire
output, and the positive or negative sign indicates right or left
justification.
-
fractionDigits
- the number of digits to print, with rounding, on the right side
of the decimal point. A negatine fractionDigits is treated like 0.
No decimal point is printed if fractionDigits is 0.
format
public static String format(Object o,
int minimumWidth)
- Create a left or right justified String from an
Object .
- Parameters:
-
o
- the Object to be printed
-
minimumWidth
- |minimumWidth| is the minimum number of characters in the entire
output, and the positive or negative sign indicates right or left
justification.
format
public static String format(double d,
int minimumWidth)
- Create a left or right justified String from a double value.
- Parameters:
-
d
- the number to be printed
-
minimumWidth
- |minimumWidth| is the minimum number of characters in the entire
output, and the positive or negative sign indicates right or left
justification.
format
public static String format(long n,
int minimumWidth)
- Create a left or right justified String from a long value.
- Parameters:
-
n
- the number to be printed
-
minimumWidth
- |minimumWidth| is the minimum number of characters in the entire
output, and the positive or negative sign indicates right or left
justification.