public class EasyReader
extends java.io.PushbackReader
EasyReader
object has a small collection of methods for
reading some primitive data values from an input stream or file.
Limitations:
If an IOException
or FileNotFoundException
occurs
during any operation, then the
EasyReader
prints an error message and halts the program.
The exceptions is not passed back to the calling program,
so the calling program does not need to catch any exceptions.
Example:
This example declares an EasyReader
that is attached to the
keyboard input (System.in
). It then uses
doubleQuery
to ask the user to enter a double number. The
square of this double number is then printed:
The
import edu.colorado.io.EasyReader
...
EasyReader stdin = new EasyReader(System.in); // Attaches to keyboard
double d;
d = stdin.doubleQuery("Please type a double value: ");
System.out.println("The square of that is: " + d*d);
EasyReader
class includes:
(1) Three constructors to create an EasyReader
from an
InputStream
, from an InputStreamReader
, or from
a file name. For example, to create an EasyReader
from
System.in
:
EasyReader stdin = new EasyReader(System.in);
(2) Query methods: The names of these methods end with "Query".
Each method prints a prompt (which is a String parameter) and then reads
one line of input, converting the line to some type (char, double,
int, or String). All of the query methods reject improperly formatted
input lines (such as a 1.5 for an integer input). When an input line is
rejected, the method prompts the user for a correctly formatted input.
This continues until an input line is typed in the correct format.
(3) Peek method: This method returns the next character of the input
without actually reading it.
(4) Input methods: The names of these methods end with "Input" or
"InputLine". The methods read input and convert it to some type
(char, double, int, or String). The "Input" methods just read the
data, and the "InputLine" methods read the data and then discard the rest
of the line.
(5) Boolean methods: The names of these methods begin with "is". They
return various information about the input status.
(6) The methods skipLine and ignore. They
read and throw away various input.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/edu/colorado/io/EasyReader.java
FormatWriter
Constructor and Description |
---|
EasyReader(java.io.InputStream in)
Initialize this
EasyReader so that it reads from an
InputStream . |
EasyReader(java.io.InputStreamReader isr)
Initialize this
EasyReader so that it reads from an
InputStreamReader . |
EasyReader(java.lang.String name)
Initialize this
EasyReader so that it reads from a
specified file. |
Modifier and Type | Method and Description |
---|---|
char |
charInput()
Read a character from this
EasyReader . |
char |
charInputLine()
Read a character from a complete line of this
EasyReader . |
char |
charQuery(java.lang.String prompt)
Print a prompt, then read and return a character from this
EasyReader . |
double |
doubleInput()
Read a
double number from this EasyReader . |
double |
doubleInputLine()
Read a double value from a complete line of this
EasyReader . |
double |
doubleQuery(java.lang.String prompt)
Print a prompt, then read and return a double value from this
EasyReader . |
void |
ignore()
Read and discard one character.
|
int |
intInput()
Read an integer from this
EasyReader . |
int |
intInputLine()
Read an integer from a complete line of this
EasyReader . |
int |
intQuery(java.lang.String prompt)
Print a prompt, then read and return an integer from this
EasyReader . |
boolean |
isEOF()
Determine whether this
EasyReader has reached the
end-of-file. |
boolean |
isEOLN()
Determine whether the next input character is an end-of-line.
|
boolean |
isFormatProblem()
Determine whether there was an incorrectly formatted input to the most
recent input operation.
|
static void |
main(java.lang.String[] args)
A demonstration program.
|
static void |
pause(long milliseconds)
Make the computation pause for a specified number of milliseconds.
|
char |
peek()
Peek ahead at the next character from this
EasyReader
(but don't read it). |
boolean |
query(java.lang.String prompt)
Print a prompt, then read and return a YES/NO answer from this
EasyReader . |
void |
skipLine()
Read and discard the rest of the current input line.
|
java.lang.String |
stringInput()
Read a
String (up to whitespace) from this
EasyReader . |
java.lang.String |
stringInputLine()
Read a
String from a complete line of this
EasyReader . |
java.lang.String |
stringQuery(java.lang.String prompt)
Print a prompt, then read and return a
String from this
EasyReader . |
public EasyReader(java.io.InputStream in)
EasyReader
so that it reads from an
InputStream
.in
- an InputStream
that this EasyReader
will read from
Postcondition:
This EasyReader
has been initialized so that its
subsequent input comes from the specified InputStream
.
Example:
EasyReader stdin = new EasyReader(System.in);
public EasyReader(java.lang.String name)
EasyReader
so that it reads from a
specified file.name
- the name of the file that this EasyReader
will read from
Postcondition:
This EasyReader
has been initialized so that its
subsequent input comes from the specified file.
If the file does not exist, then an error message is printed
to System.err and the program exits.
Example:
EasyReader stdin = new EasyReader("foo.txt");
public EasyReader(java.io.InputStreamReader isr)
EasyReader
so that it reads from an
InputStreamReader
.isr
- an InputStreamReader
that this EasyReader
will read from
Postcondition:
This EasyReader
has been initialized so that its subsequent
input comes from the specified InputStreamReader
.public char charInput()
EasyReader
.public char charInputLine()
EasyReader
.charInput()
with an added
activation of skipLine()
just before returning.public char charQuery(java.lang.String prompt)
EasyReader
.prompt
- a prompt to printSystem.out
. Then a
character has been read and returned with charInputLine
.public double doubleInput()
double
number from this EasyReader
.double
number that's been read
Input Method:
An attempt is made to read the following items into a
String
:
(1) Zero or more whitespace characters (which are discarded);
(2) An optional + or - sign.
(3) A sequence of digits that form the integer part of the number.
(4) If the next character is a decimal point, then it is read
along with a sequence of digits that form the fractional part
of the number.
(5) If the next character is 'e' or 'E', then it is read along
with an optional +/- sign and digits that form the exponent
part of the number.
After these items, there may be a non-digit delimiter, or the
end-of-file may appear after the number. The delimiter (or EOF)
is not read.
Conversion: The above items are converted to a double value
using Double.valueOf
.
Format Problems:
If a NumberFormatException
occurs, then the method returns Double.NaN
and an immediate
activation of isFormatProblem()
will return true.public double doubleInputLine()
EasyReader
.doubleInput( )
with an added
activation of skipLine( )
just before returning.public double doubleQuery(java.lang.String prompt)
EasyReader
.prompt
- a prompt to printSystem.out
. Then a double
value has been read and returned with doubleInputLine
.
Format Problems:
If doubleInputLine
encounters a format problem, but
!isEOF()
, then the user is prompted to type a new
input line until a correct double value is provided. If end-of-file
is reached, then the method returns Double.NaN
and
an immediate activation of isFormatProblem()
will return
true.public void ignore()
public int intInput()
EasyReader
.String
:
(1) Zero or more whitespace characters (which are discarded);
(2) An optional + or - sign.
(3) A sequence of digits that form the actual integer.
There may be a non-digit delimiter after the integer, or the
end-of-file may appear after the integer. The delimiter (or EOF)
is not read.
Conversion: The above items are converted to an integer value
using Integer.parseInt
.
Format Problems:
If a NumberFormatException
occurs, then the method returns Integer.MIN_VALUE
and an
immediate activation of isFormatProblem()
will return true.public int intInputLine()
EasyReader
.intInput( )
with an added
activation of skipLine( )
just before returning.public int intQuery(java.lang.String prompt)
EasyReader
.prompt
- a prompt to printSystem.out
. Then an
integer has been read and returned with intInputLine
.
Format Problems:
If intInputLine
encounters a format problem, but
!isEOF()
, then the user is prompted to type a new
input line until a correct int value is provided. If end-of-file
is reached, then the method returns Integer.MIN_VALUE
and an immediate activation of isFormatProblem()
will return
true.public boolean isEOF()
EasyReader
has reached the
end-of-file.EasyReader
has reached the end of file
(reading all characters up to but not including EOF), then the return
value is true; if an attempt to read causes an IOException,
then the return value is also
true; otherwise the return value is false.
Note:
A user at the keyboard indicates EOF for standard input by typing
ctrl-z (MS Windows) or ctrl-c (Unix). For an interactive user, this
method does pause until the
user provides some input or indicates end-of-file by pressing ctrl-z.
Example:
Read one integer per line from standard input until EOF, then print
the sum of all the integers:
EasyReader stdin = new EasyReader(System.in);
int sum = 0;
System.out.println("Type one int per line and press ctrl-z to end:");
while (!stdin.isEOF( ))
sum += stdin.intInputLine( );
System.out.println("Total sum: " + sum);
public boolean isEOLN()
isEOF()
, then the
return value is also true; if an attempt to read causes an
IOException
, then
the return value is also true; otherwise the return value is false.public boolean isFormatProblem()
doubleInput, intInput
doubleInputLine, intInputLine
doubleQuery, intQuery
public static void pause(long milliseconds)
milliseconds
- the number of milliseconds to pause
Postcondition:
The computation has paused for the specified time.public char peek()
EasyReader
(but don't read it).EasyReader
. If there is no next character (because of
the end-of-file marker), then the return value is '\0'.public boolean query(java.lang.String prompt)
EasyReader
.prompt
- a prompt to printstringQuery(prompt)
has been called to ask a question
and read the answer, which is considered true if it begins with
"Y" or "y" and false if it begins with "N" or "n". If the answer did
not begin with a lower- or upper-case Y or N, then the process is
repeated until a correct Yes/No answer is provided. If EOF is reached,
then false is returned.public void skipLine()
public java.lang.String stringInput()
String
(up to whitespace) from this
EasyReader
.String
that's been read
Format:
Whitespace has been skipped, and then a string has been read
up to but not including the next whitespace character.public java.lang.String stringInputLine()
String
from a complete line of this
EasyReader
.String
that's been read
Format:
An entire line of characters has been read up to and including the end
of the current line (or the end-of-file). All characters before the
end are returned in a String
.public java.lang.String stringQuery(java.lang.String prompt)
String
from this
EasyReader
.prompt
- a prompt to printSystem.out
. Then a
String
has been read and returned with
stringInputLine
.public static void main(java.lang.String[] args)
java edu.colorado.io.EasyReader
args
- not used in this implementation