/*
 * Stdin.java
 * A class crated to simplify input from the keyboard in Java.
 * Created on September 26, 2003, 1:34 PM
 */

/**
 * Stdin.java
 * A class crated to simplify input from the keyboard in Java: 
 *
 *It begins by reading an entire string, then each time we want 
 a new thing it will try and read it. When it runs out of chars 
 on the current string, it will read a new string.
 *
 *It is intended to be used as a static class with no instances.
 *
 * @author  Katrin Becker
 * @version 1.0
 */
import java.lang.*;
import java.io.*;
import java.text.*;
public class Stdin {

    /** this is the java standard input stream stuff */
    static InputStreamReader r = new InputStreamReader(System.in);
    /** this is the java standard input stream stuff */
    static BufferedReader br = new BufferedReader(r);
    
    /** holds the current line of input - gets 'refreshed' when necessary */
    static String line = "";
    
    /** our current position in the input line */
    static int pos = 0;
    
    /** flag set if exception was thrown so user can check */
    static boolean ok = true;
    /** returns false if an exception was thrown so user can check
     */
    public static boolean OK() { return ok; }
    
    /** Constructor: Creates new Stdin Since this class is 
     intended to be used without instantiation, the constructor does nothing. */
    public Stdin() {
    }

    //========================================================================
    /** returns true if there are no more characters to read on this input line
     */
    public static boolean doneLine() {
        return (pos == line.length()); 
    }

    //========================================================================
    /** Read an entire string form standard system input 
     */
    public static void readLine()
    {
        try {
            ok = true;
            line = br.readLine();
            pos = 0;
        }
        catch (Exception e) {
            line = ""; // just set something up
            pos = 0;
            ok = false;
        }
    }// end readLine
    
    //========================================================================
    /** this one just returns the next character. */
    public static char getChar() {
        ok = true;
        if (doneLine()) readLine();
        return line.charAt(pos++);
    } // end getChar

    //========================================================================
    /** we want to be able to skip over while space without blowing up */
    public static void skipWS()
    {
        while ((pos < line.length()) &&
                ((line.charAt(pos) == ' ') ||
                 (line.charAt(pos) == '\t') ||
                 (line.charAt(pos) == '\r') ||
                 (line.charAt(pos) == '\n')) )
            pos++;
    } // end skipWS

    //========================================================================
    /** skip ALL white space; including newlines <br>
	(i.e. it reads the next line if necessary) */
    public static void skipLeadingSpace() {
        skipWS();
        if (doneLine()) readLine();
        skipWS();
    } // skipLeadingSpace        
    
    //========================================================================
    /** gets a whole new line from beginning to end.
     *Anything on the previous line is discarded */
    public static String getLine() {
        ok = true;
        readLine();
        pos = line.length(); // so next get forces another read
        return line;
    } // end getLine
    
    //========================================================================
    /** this routine gets rid of anything left in the line */
    public static void flush() {
        pos = line.length();
        line = "";
        ok = true;
    } // end flush
    
    //========================================================================
    /** read an integer: */
    public static int getInt()
    {
        String numS = "";
        ok = true;
        skipLeadingSpace();
        
        // leading minus sign?
        if (line.charAt(pos) == '-')
           numS += line.charAt(pos++);
        // copy the digits
        while ((pos < line.length()) &&
                (line.charAt(pos) >= '0')&&(line.charAt(pos) <= '9'))
        {
            // copy the char into the num-string NAD increment the
            // position 'pointer'
            numS += line.charAt(pos++);
        }         
        // convert it:
        try {
            return Integer.parseInt(numS);
        } catch (Exception e) {
            ok = false;
            return 0;
        }
    } // end getInt
    
    //========================================================================
    /** read a real: */
    public static double getDouble()
    {
        String numS = "";        
        ok = true;
        skipLeadingSpace();

        // leading minus sign?
        if (line.charAt(pos) == '-')
           numS += line.charAt(pos++);
        // copy the digits - note this will not avoind all illegal real values
        while ((pos < line.length()) &&
               (((line.charAt(pos) >= '0')&&(line.charAt(pos) <= '9'))
                  || (line.charAt(pos) == '.')))
        {
            // copy the char into the num-string NAD increment the
            // position 'pointer'
            numS += line.charAt(pos++);
        }        
        // convert it:
        try {
            return Double.parseDouble(numS);
        } catch (Exception e) {
            ok = false;
            return 0.0;
        }
    } // end getDouble

    //========================================================================
   /** this is what I want printed if someone decides to print this object:<br>
    Includes:<br>
    <ul>
    <li>the current input line<br>
    <li>the index position of the next <b>unread</b> character<br> 
    </ul>*/
    public static void dump() {
        System.out.println( "Line: |" + line + "|, pos = " + pos);
    }    
} // end Stdin
