Provides a simple interface for standard text reading operations from an input stream. Converts all IOExceptions to RuntimeExceptions, so it should be used only with stable input streams like System.in. Uses lazy evaluation (delaying the reading of the beginning of a new line until necessary) so as to function properly when used with console input. Unlike the standard java.io.BufferedReader, it returns a correct value for ready() with console reading (returning false only if the user enters an end-of-file from the console). Includes a one-character peek ahead facility.
TextReader's behavior is similar to that of Pascal's standard input. It has three utilities for processing the input one token at a time (one for words and two for numbers). As in Pascal, these token-processing methods skip leading whitespace, but unlike Pascal, they also skip trailing whitespace on the current input line. Skipping the trailing space allows one to write token-processing programs without worrying about trailing whitespace at the end of a line that might imply that more input is present when it really isn't.
TextReader also provides utilities for reading the input one character at a time or one line at a time, in which case whitespace is preserved. As noted above, it also has a utility for peeking one character ahead in the stream. There is also a method for skipping whitespace. A programmer can combine these utilities to define specialized behavior.
To simplify differences between operating systems, TextReader skips a "\r" character if it sees it so that the two-character sequence "\r\n" becomes a single "\n" character. It also inserts a virtual "\n" before end-of-file if one is not present.
Below is an example of how to construct and manipulate a reader for interactive console input.
The code above would execute something like this (user input underlined):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));
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
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.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
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 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();
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.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();
Questions about this class can be sent to Stuart Reges (reges@cs.arizona.edu). @version 1.1, 1/7/01 @author Stuart Regesimport java.io.*;
|
|