package lab2;

import java.util.*;
import java.io.IOException;

/**
 * This class will be enhanced so that it handles exceptions better. 
 * As it stands it is vulnerable to IOExceptions, NumberFormatExceptions,
 * and illogical input.
 */
public class InputTesterRobust {

    /**
     * This method relies on a private helper method getAge that may
     * throw an IOException.
     * @param args Not used.
     */
    public static void main(String[] args) throws IOException {
	int age = getAge();
	age++;
	System.out.println("Next year, you'll be " + age);
    }

    /**
     * This method gets a line of input from the user that is supposed
     * to be the user's age as an integer.  A special input, namely
     * the string "ioerror" triggers a program-thrown IOException for
     * testing purposes.
     */
    private static int getAge() throws IOException {
	Scanner in = new Scanner(System.in);
	System.out.println("How old are you? ");
	String input = in.nextLine();
	if (input.equals("ioerror"))
	    throw new IOException
		("\nThis is a bogus I/O exception for testing.");
	return Integer.parseInt(input);
    }

}