import java.io.*;

/**
 * This class holds static methods to talk to the user.  All exception
 * handling is encapsulated in these methods.  Also, this class
 * has an encapsulated sleep() method which will interrupt a thread
 * properly.
 */
class Utilities {

	// Use stdin.readLine() to get Input from the user
	public static BufferedReader stdin = new BufferedReader(new 
										InputStreamReader(System.in));

    // Gets a valid int from the user
    public static int getInteger(String prompt) {
    
        int number = 0;
        boolean valid = false;

        while(!valid) {
            System.out.print(prompt);
            try {
                number = Integer.parseInt(stdin.readLine());
                valid = true;
            }
            catch (NumberFormatException nfe) {
                System.out.println("Invalid Input.  Try again.");
            }
            catch (IOException ioe) {
                System.out.println("Error in Input.  Terminating.");
                System.exit(0);
            }
        }
        return number;
    }
    
    // Gets a valid String from the user
    public static String getString(String prompt) {
    
        String s = "";
        boolean valid = false;

        while(!valid) {
            System.out.print(prompt);
            try {
                s = stdin.readLine();
                valid = true;
            }
            catch (IOException ioe) {
                System.out.println("Error in Input.  Terminating.");
                System.exit(0);
            }
        }
        return s;
    }

    // Lets the thread sleep without catching exceptions. If interrputed
    // it will call the current Thread's interrupt method.
    public static void sleep(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
