//
// Classes for simple input from standard input and files.
//
// Originally written by:
//   B. A. Sanders, 7/1997; modified 12/1997.
// Modified by:
//   B. L. Massingill, 2/1998 and 4/2002.
//
// Main program illustrates use.
//
// All routines throw an SimpleReaderEOFException if an end of file 
//   is encountered.
// IOExceptions are converted to SimpleReaderIOExceptions.
// SimpleReaderEOFExceptions and SimpleReaderIOExceptions are both 
//   RuntimeExceptions that need not be declared in clients.
// Input entry is flexible, but if an illegal value is encountered,
//   the remaining entries on the same line are lost.
//
import java.io.*;
import java.util.*;

public class SimpleReader {

    private StringTokenizer st;
    private BufferedReader br;

    // Constructor with no parameters sets up to read from stdin.
    public SimpleReader() {
	br = new BufferedReader(new InputStreamReader(System.in));
    }

    // Constructor with a String parameter (filename) sets up to 
    //   read from file.
    public SimpleReader(String fileName) {
	try{
	    br = new BufferedReader(new FileReader(fileName));
	}
	catch(FileNotFoundException e) {
	    throw new SimpleReaderIOException("File " + fileName + " not found");
	}
     }

    // Public methods.

    public int readInt() throws SimpleReaderIOException {
	try {
	    return Integer.parseInt(nextToken());
	}
	catch (NumberFormatException e) {
	    flushLine();
	    throw new SimpleReaderIOException("Input not an integer");
       }
    }

    public long readLong() throws SimpleReaderIOException {
	try {
	    return Long.parseLong(nextToken());
	}
	catch (NumberFormatException e) {
	    flushLine();
	    throw new SimpleReaderIOException("Input not an integer");
       }
    }

    public boolean readBoolean() throws SimpleReaderIOException {
	String s = readString();
        if (s.equals("true")) return true;
        else if (s.equals("false")) return false;
	else {
	    flushLine();
	    throw new SimpleReaderIOException("Input not a boolean value");
       }
    }
  
    public String readString() {
	return nextToken();
    }

    public float readFloat() throws SimpleReaderIOException {
	try {
	    return Float.parseFloat(nextToken());
	}
	catch (NumberFormatException e) {
	    flushLine();
	    throw new SimpleReaderIOException("Input not a float");
       }
    }

    public double readDouble() throws SimpleReaderIOException {
	try {
	    return Double.parseDouble(nextToken());
	}
	catch (NumberFormatException e) {
	    flushLine();
	    throw new SimpleReaderIOException("Input not a double");
       }
    }

    private String nextToken() {
	try {
	    int test0 = 0;
	    while (st == null || !st.hasMoreTokens()) {
		String s = br.readLine();
		if (s == null)
		    throw(new SimpleReaderEOFException("Encountered EOF"));
		st = new StringTokenizer(s);
	    }
	    return st.nextToken();
        } 
	catch (IOException e) {
	    throw(new SimpleReaderIOException());
	}
    }
     
    private void flushLine(){
	st = null;
    }

    // Main method for testing.
    public static void main(String[] args) throws IOException {

	// test input from standard input
	SimpleReader stdin = new SimpleReader();
	System.out.println("Enter two integers");
	int i = stdin.readInt(); 
	long j = stdin.readLong();
	System.out.println("You entered " + i + " and " + j);
	System.out.println("Enter a boolean");
	boolean b = stdin.readBoolean();
	System.out.println("You entered " + b);
	System.out.println("Enter two real numbers");
	float fl = stdin.readFloat(); 
	double d = stdin.readDouble();
	System.out.println("You entered " + fl + " and " + d);
	System.out.println("Enter a string");
	String str = stdin.readString();
	System.out.println("You entered " + str);
	
	// test string input from file
	System.out.println("\nEnter file containing strings");
	String fname1 = stdin.readString();
	SimpleReader f1 = new SimpleReader(fname1);
	System.out.println("File contains:");
	try {
	    while (true) 
		System.out.println(f1.readString());
	}
	catch (SimpleReaderEOFException e) { }

	// test integer input from file
	System.out.println("\nEnter file containing integers");
	String fname2 = stdin.readString();
	SimpleReader f2 = new SimpleReader(fname2);
	int sum = 0;
	try {
	    while (true) 
		sum += f2.readInt();
	}
	catch (SimpleReaderEOFException e) { }
	System.out.println("Sum = " + sum);
    }
}

class SimpleReaderIOException extends RuntimeException {
    SimpleReaderIOException(String s)  { super(s); }
    SimpleReaderIOException() { }
}

class SimpleReaderEOFException extends SimpleReaderIOException {
    SimpleReaderEOFException(String s)  { super(s); }
    SimpleReaderEOFException() { }
}