public class TextReader extends Object
TextReader console = new TextReader(System.in);
System.out.print("What is your name? ");
String name = console.readLine();
System.out.print("Give me two positive numbers, " + name + " --> ");
double x = console.readDouble();
double y = console.readDouble();
System.out.println(x + " to the " + y + " power = " + Math.pow(x, y));
System.out.print("Give me a positive integer, " + name + " --> ");
int n = console.readInt();
System.out.println("2 to the " + n + " = " + (int) Math.pow(2, n));
The code above would execute something like this (user input underlined):
By default the token-processing methods like readInt consume the newline character at the end of each line, which assumes that newline characters are not significant. There is a utility that allows a programmer to specify that newline characters are significant, in which case they are left in the input stream. For example, the code below reads exactly one line of input, adding up the integers on the line.What is your name? <u>John Boy</u> Give me two positive numbers, John Boy --> <u>3.4 2.9</u> 3.4 to the 2.9 power = 34.77673927667935 Give me a positive integer, John Boy --> <u>14</u> 2 to the 14 = 16384
input.eolIsSignificant(true);
input.skipWhite(false); // in case the line contains just whitespace
sum = 0;
while (input.peek() != '\n')
sum += input.readInt();
input.readChar(); // to skip past the newline
To construct a reader using a file, the potential I/O error must be caught
because, for example, the specified file might not exist. Below is an example
of how to construct and manipulate a file reader using a specific file name
(in this case, project.dat). If an error occurs, the program exits with a
runtime exception.
TextReader input;
try {
input = new TextReader(new FileInputStream("project.dat"));
} catch (IOException e) {
throw new RuntimeException(e.toString());
}
// if you made it to here, then the file exists and is readable
int sum = 0;
while (input.ready())
sum += input.readInt();
Below is a variation where the user is prompted for a file name and the
program loops until a legal file name is given.
TextReader console = new TextReader(System.in);
TextReader input;
boolean done = false;
do {
System.out.print("input file name? ");
String name = console.readLine();
try {
input = new TextReader(new FileInputStream(name));
done = true;
} catch (IOException e) {
System.out.println(e + ", try again");
}
} while (!done);
int sum = 0;
while (input.ready())
sum += input.readInt();
The previous two code examples use FileInputStream and IOException, which
would require you to include the line below at the beginning of your class
file.
import java.io.*;
| Constructor and Description |
|---|
TextReader(InputStream input)
Create a text-reading stream.
|
| Modifier and Type | Method and Description |
|---|---|
void |
eolIsSignificant(boolean flag)
Determine whether or not ends of line are treated as tokens.
|
char |
peek()
Peek ahead one character.
|
char |
readChar()
Read a single character (including whitespace).
|
double |
readDouble()
Read the next token as a double.
|
int |
readInt()
Read the next token as an integer.
|
String |
readLine()
Reads a line of text.
|
String |
readWord()
Read the next token delineated by whitespace.
|
boolean |
ready()
Tell whether this stream is ready to be read.
|
void |
skipWhite(boolean skipEoln)
Skip whitespace.
|
public TextReader(InputStream input)
input - the input stream to read frompublic char readChar()
RuntimeException - if end-of-filepublic char peek()
RuntimeException - if end-of-filepublic boolean ready()
public String readLine()
RuntimeException - if end-of-filepublic void eolIsSignificant(boolean flag)
flag - whether end-of-line characters should be left in streampublic void skipWhite(boolean skipEoln)
skipEoln - whether or not to skip end-of-line characterspublic String readWord()
public int readInt()
RuntimeException - if no token or next token not an integerpublic double readDouble()
RuntimeException - if no token or next token not a doubleCopyright © 2016. All Rights Reserved.