/** TestInterpretInput operation by reading one of each value ** supported by its public interface either from the console ** or a file indicated on the command line. **/ import java.io.FileNotFoundException; import java.io.EOFException; import java.io.IOException; public class TestInputInterpreter { InputInterpreter inputSource = null; /** Create an InputInterpreter associated with either the ** console or a file whose name is given in the srgument array. **/ private void setUpInput(String args[]) throws FileNotFoundException { if (args.length == 0) { // Read and interpret console input inputSource = new InputInterpreter(); } else { // Read and interpret input from file indicated in args inputSource = new InputInterpreter(args[0]); } } /** Read and interpret the contents of the input source. **/ private void readInput() { boolean doPrompts = inputSource.isConsole(); try { while (true) { if (doPrompts) System.out.println("Enter a string, e.g., your name"); // Declare a String and then ask inputSource to read a String // and assign it to the String object you've declared if (doPrompts) System.out.println("Enter an int, e.g., your age"); // Declare an int and then ask inputSource to read an int // value and assign it to the int variable you've declared if (doPrompts) System.out.println("Enter digits for a double, e.g., taxes paid"); // Declare a double and then ask inputSource to read a // double value and assign it to the double variable // you've declared // Report the String, int and double values you've read in } } catch (EOFException eof) { System.out.println("caught end of file"); } } // Read records from the indicated file public int readRecords( String filename ) { int numRecords = 0; int recordsRead = 0; try { // Read and interpret input from file indicated in filename inputSource = new InputInterpreter(filename); // Assume the input file consists of an initial line that has // the number of records, followed by the records themselves numRecords = inputSource.readIntValue(); // Now read that many records. Each record consists of a // String, an int value, and a double value, each on // separate lines. So the file contains // string // int // double // string // int // double // ... String stringVal; int intVal; double dblVal; for (recordsRead = 0; recordsRead < numRecords; recordsRead++ ) { stringVal = inputSource.readString(); intVal = inputSource.readIntValue(); dblVal = inputSource.readDoubleValue(); // do something useful with the three input items read, // like print them, make a new object with them ... } } catch (FileNotFoundException e) { // whoops return 0; // read 0 records } catch (EOFException e) { // got to end of file earlier than expected System.err.println("End of file after " + recordsRead + " records"); return recordsRead; } catch (IOException e) { // some other unexpected problem; have it report itself System.err.println(e); return 0; } return numRecords; } public static void main( String[] args) { TestInputInterpreter tester = new TestInputInterpreter(); try { // One of the constructors can throw a FileNotFound // exception; thus the need to enclose the following in a // try block. tester.setUpInput(args); tester.readInput(); } catch (FileNotFoundException e) { System.out.println(e); } } }