import java.io.*;
import java.util.*;

class TokenReader {



private DataInputStream theStream;
private boolean            eofReached = false;  
private String            S;
private StringTokenizer    T;                 

public TokenReader(InputStream s)
{
   theStream = new DataInputStream(s);
}

public TokenReader(DataInputStream ds)
{
   theStream = ds;
}


public TokenReader(String fileName) {
   try {
       FileInputStream fis = new FileInputStream (fileName);
       theStream = new DataInputStream(fis);
   } catch (FileNotFoundException e) {
       System.err.println ("Sorry, couldn't find file " + fileName
           + " for some reason.");
   }
}




public int readInt ()
{
   String item = "";

   if (T == null)
       refresh();

   while (true) {
       if (eofReached)
           return Integer.MIN_VALUE;

       try {
           item = T.nextToken();
           return Integer.parseInt(item.trim());
       }
       catch (NoSuchElementException e) {
           refresh();
       }
       catch (NumberFormatException e) {
           System.err.println("Integer expected but input was \"" + item +
"\".  Please try again.");
       }
   }
}

public double readDouble ()
{
   String item = "";    // next token as a string

   if (T == null)
       refresh();

   while (true) {
       if (eofReached)
           return Double.NaN;

       try {
           item = T.nextToken();
           return Double.valueOf(item.trim()).doubleValue();
       }
       catch (NoSuchElementException e) {
           refresh();
       }
       catch (NumberFormatException e) {
           System.err.println("Double expected but input was \"" + item +
"\".  Please try again.");
       }
   }
}

public String readString ()
{
   if (T == null)
       refresh();

   while (true) {
       if (eofReached)
           return "readString called after" +
" reaching end of TokenReader stream.";

       try {
           return T.nextToken();
       }
       catch (NoSuchElementException e) {
           refresh();
       }
   }
}


public String readLine ()
{
   refresh();
   if (eofReached)
       return "readLine called after reaching end of TokenReader stream.";

       String line = S;
       T = null;
       return line;
}


public boolean eof()
{
   return eofReached;
}


public void waitUntilEnter()
{
   System.out.println();
   System.out.println("Press Enter to continue.");
   try {
       theStream.read();
   } catch (java.io.IOException e) {}
}


private void refresh ()
{
       if (eofReached) {
           System.err.println("Attempt to read past"
+ " end of TokenReader stream.");
           return;
       }

       try {
           S = theStream.readLine();
       }
       catch (EOFException e) {
           eofReached = true;
           T = null;
           return;
       }
       catch (IOException e) {
           System.err.println("Unexpected error reading"
+ " TokenReader input.\nIOException: " + e);
           return;
       }

       if (S == null) {
           eofReached = true;
           T = null;
       } else
           T = new StringTokenizer(S);
}

}

