|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.io.Reader
java.io.FilterReader
java.io.PushbackReader
edu.colorado.io.EasyReader
public class EasyReader
The 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:
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:
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);
FormatWriter| Constructor Summary | |
|---|---|
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. |
|
| Method Summary | |
|---|---|
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. |
| Methods inherited from class java.io.PushbackReader |
|---|
close, mark, markSupported, read, read, ready, reset, skip, unread, unread, unread |
| Methods inherited from class java.io.Reader |
|---|
read, read |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public EasyReader(java.io.InputStream in)
EasyReader so that it reads from an
InputStream.
in - an InputStream that this EasyReader
will read from
EasyReader has been initialized so that its
subsequent input comes from the specified InputStream.
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
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.
EasyReader stdin = new EasyReader("foo.txt");public EasyReader(java.io.InputStreamReader isr)
EasyReader so that it reads from an
InputStreamReader.
in - an InputStreamReader that this EasyReader
will read from
- Postcondition:
-
This
EasyReader has been initialized so that its subsequent
input comes from the specified InputStreamReader. | Method Detail |
|---|
public char charInput()
EasyReader.
- - none
public char charInputLine()
EasyReader.
- - none
charInput() with an added
activation of skipLine() just before returning.public char charQuery(java.lang.String prompt)
EasyReader.
prompt - a prompt to print
System.out. Then a
character has been read and returned with charInputLine.public double doubleInput()
double number from this EasyReader.
- - none
String:
Double.valueOf.
NumberFormatException
occurs, then the method returns Double.NaN and an immediate
activation of isFormatProblem() will return true.public double doubleInputLine()
EasyReader.
- - none
doubleInput( ) with an added
activation of skipLine( ) just before returning.public double doubleQuery(java.lang.String prompt)
EasyReader.
prompt - a prompt to print
System.out. Then a double
value has been read and returned with doubleInputLine.
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()
- - none
public int intInput()
EasyReader.
- - none
String:
Integer.parseInt.
NumberFormatException
occurs, then the method returns Integer.MIN_VALUE and an
immediate activation of isFormatProblem() will return true.public int intInputLine()
EasyReader.
- - none
intInput( ) with an added
activation of skipLine( ) just before returning.public int intQuery(java.lang.String prompt)
EasyReader.
prompt - a prompt to print
System.out. Then an
integer has been read and returned with intInputLine.
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.
- - none
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.
EasyReader stdin = new EasyReader(System.in);
int sum = 0;
System.out.println("Type one int per line & press ctrl-z to end:");
while (!stdin.isEOF( ))
sum += stdin.intInputLine( );
System.out.println("Total sum: " + sum);
public boolean isEOLN()
- - none
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()
- - none
doubleInput, intInput
doubleInputLine, intInputLine
doubleQuery, intQuery
public static void pause(long milliseconds)
milliseconds - the number of milliseconds to pause
public char peek()
EasyReader
(but don't read it).
- - none
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 print
stringQuery(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()
- - none
public java.lang.String stringInput()
String (up to whitespace) from this
EasyReader.
- - none
String that's been read
public java.lang.String stringInputLine()
String from a complete line of this
EasyReader.
- - none
String that's been read
String.public java.lang.String stringQuery(java.lang.String prompt)
String from this
EasyReader.
prompt - a prompt to print
System.out. Then a
String has been read and returned with
stringInputLine.public static void main(java.lang.String[] args)
java edu.colorado.io.EasyReader
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||