import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.EOFException; import java.io.FileInputStream; import java.util.StringTokenizer; import java.awt.FileDialog; import java.awt.Frame; // // // Reader409 // // /** * A class for reading data from a text file. * Blanks (and tabs) are ignored. * Includes methods for reading an int, a double, etc. * * @author Paul Chew for CS409, Feb 1999. */ public class Reader409 { private String line = ""; // Whatever is left of the current input line. private BufferedReader in; // The stream that we read from. private boolean isEOL = true; // True iff we are at the end-of-line. /** * Constructor. * @param fileName the name of an input file * @exception FileNotFoundException if file cannot be opened */ public Reader409 (String fileName) throws FileNotFoundException { in = new BufferedReader(new FileReader(fileName)); } /** * Constructor. * @param in an InputStream to read from */ public Reader409 (InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } /** * Constructor. * Uses a FileDialog to determine the appropriate file. * @exception FileNotFoundException if file cannot be opened */ public Reader409 () throws FileNotFoundException { FileDialog fd = new FileDialog(new Frame(),"Select an input file"); fd.move(100,100); fd.show(); in = new BufferedReader(new FileReader(fd.getFile())); } /** * Update the current input line. * Lines containing only whitespace are ignored. * @exception EOFException if attempt is made to read past EOF * @exception IOException if an IO error occurs */ private void update () throws IOException { isEOL = false; line = line.trim(); while (line.equals("")) { line = in.readLine(); if (line == null) throw new EOFException(); line = line.trim(); } } /** * Read a single char. Whitespace is ignored. * @return the char read * @exception EOFException if attempt is made to read past EOF * @exception IOException if an IO error occurs */ public char readChar () throws IOException { update(); char c = line.charAt(0); line = line.substring(1); return c; } /** * Read a blank-delimited word. * A word here is anything that doesn't contain blanks. * @return the word read * @exception EOFException if attempt is made to read past EOF * @exception IOException if an IO error occurs */ public String readWord () throws IOException { update(); StringTokenizer tokenizer = new StringTokenizer(line); String word = tokenizer.nextToken(); line = line.substring(word.length()); return word; } public String readData () throws IOException { update(); StringTokenizer tokenizer = new StringTokenizer(line); String word = tokenizer.nextToken(); line = line.substring(word.length()); return word; } /** * Read an int. * @return the int read * @exception EOFException if an attempt is made to read past EOF * @exception IOException if an IO error occurs * @exception NumberFormatException if number is malformed */ public int readInteger () throws IOException { Integer i = new Integer(readWord()); return i.intValue(); } /** * Read a line of ints. * @return an array (int[]) containing the numbers read * @exception EOFException if an attempt is made to read past EOF * @exception IOException if an IO error occurs * @exception NumberFormatException if number is malformed */ public int[] readIntegers () throws IOException { update(); StringTokenizer tokenizer = new StringTokenizer(line); line = ""; int[] result = new int[tokenizer.countTokens()]; for (int i = 0; i < result.length; i++) { result[i] = (new Integer(tokenizer.nextToken())).intValue(); } return result; } /** * Read a double. * @return the double read * @exception EOFException if an attempt is made to read past EOF * @exception IOException if an IO error occurs * @exception NumberFormatException if number is malformed */ public double readDouble () throws IOException { Double d = new Double(readWord()); return d.doubleValue(); } /** * Read a line of doubles. * @return an array (double[]) containing the numbers read * @exception EOFException if an attempt is made to read past EOF * @exception IOException if an IO error occurs * @exception NumberFormatException if number is malformed */ public double[] readDoubles () throws IOException { update(); StringTokenizer tokenizer = new StringTokenizer(line); line = ""; double[] result = new double[tokenizer.countTokens()]; for (int i = 0; i < result.length; i++) { result[i] = (new Double(tokenizer.nextToken())).doubleValue(); } return result; } /** * Read a line of input (or the rest of the current line). * @return the line of input * @exception EOFException if an attempt is made to read past EOF * @exception IOException if an IO error occurs */ public String readLine() throws IOException { String result = isEOL ? in.readLine() : line; if (result == null) throw new EOFException(); line = ""; isEOL = true; return result; } /** * Close the reader. * @exception IOException if an IO error occurs */ public void close () throws IOException { in.close(); } /** * Return a String that represents the given point (represented as an * array of ints). * @param point an array representing an integer point * @return a String representing the point * @exception IllegalArgumentException if the array has 0 length */ public static String toString (int[] point) { if (point.length < 1) throw new IllegalArgumentException("Point with no coordinates!"); StringBuffer sb = new StringBuffer("(" + point[0]); for (int i = 1; i < point.length; i++) { sb.append("," + point[i]); } return sb.append(")").toString(); } /** * Main program. Used for testing, but otherwise useless. */ public static void main (String[] args) throws IOException { Reader409 in = new Reader409(System.in); while (true) try { System.out.print("? "); char c = in.readChar(); switch (c) { case 'w': { System.out.println("word = " + in.readWord()); in.readLine(); break; } case 'i': { System.out.println("ints = " + toString(in.readIntegers())); in.readLine(); break; } case 'd': { System.out.println("double = " + in.readDouble()); in.readLine(); break; } case 'q': { System.exit(0); } default: { System.out.println("What?"); in.readLine(); break; } } } catch (NumberFormatException e) { System.out.println("Badly formed number."); continue; } } }