// A class that helps reads values of a primitive type from the keyboard.
// Barry Cornelius, 19 June 2000
import java.io.   BufferedReader;
import java.io.   InputStreamReader;
import java.util. StringTokenizer;
public class KeyboardInput
{
   private final BufferedReader iKeyboard =
         new BufferedReader(new InputStreamReader(System.in));
   private StringTokenizer iTokens = null;

   public KeyboardInput()
   {
   }

   // return MAX_VALUE if an integer cannot be read
   public int nextInt()
   {
      try
      {
         final String tTokenString = iGetToken();
         return Integer.parseInt(tTokenString);
      }
      catch(final Exception pException)
      {
         return Integer.MAX_VALUE;
      }
   }

   // return MAX_VALUE if a double cannot be read
   public double nextDouble()
   {
      try
      {
         final String tTokenString = iGetToken();
         return Double.parseDouble(tTokenString);
      }
      catch(final Exception pException)
      {
         return Double.MAX_VALUE;
      }
   } //BJCHEREFIRST
   private String iGetToken() throws Exception
   {
      while ( iTokens==null || ! iTokens.hasMoreTokens() )
      {
         final String tLine = iKeyboard.readLine();
         iTokens = new StringTokenizer(tLine);
      }
      // iTokens!=null && iTokens.hasMoreTokens()
      return iTokens.nextToken();
   }
}
