import java.util.Scanner;

/** This class will provide an example of when
	you might build a method.
	
	@author Gaddis and Nancy Harris
	@version V2 10/02/2008  
*/

public class Example
{
	/** this method will read in a series of 2
	 *  numbers and provide the sum and average
	 *  of the 2.  If a bad value is input, 
	 *  display an error message and reprompt.
	 *  Exit the program on 2 bad values in a row.
	 *
	 * @param args Unused in this application
	 */
	public static void main(String [] args)
	{
		final int NUMBER = 2;
		
		double num1;
		double num2;
		double average;
				
		Scanner keyboard;
	
		// instantiate keyboard
		keyboard = new Scanner(System.in);
		
		// read in each value
		
		// num1
		System.out.print("Enter number 1: ");
		
		// error check
		if (keyboard.hasNextDouble())
		{
			num1 = keyboard.nextDouble();
		}
		else
		{
			System.out.println("\nBad value " + keyboard.next() +
				 ". Please try again");
			System.out.print("Enter number 1: ");
			if (keyboard.hasNextDouble())
			{
				num1 = keyboard.nextDouble();
			}
			else
			{
				num1 = 0;
				System.out.println("\nBad value " + keyboard.next() +
				 ". Exiting the program");
				System.exit(1);
			}
		}
		
		// num2
		System.out.print("Enter number 2: ");
		
		// error check
		if (keyboard.hasNextDouble())
		{
			num2 = keyboard.nextDouble();
		}
		else
		{
			System.out.println("\nBad value " + keyboard.next() +
				 ". Please try again");
			System.out.print("Enter number 2: ");
			if (keyboard.hasNextDouble())
			{
				num2 = keyboard.nextDouble();
			}
			else
			{
				num2 = 0;
				System.out.println("\nBad value " + keyboard.next() +
				 ". Exiting the program");
				System.exit(2);
			}
		}
		// Average the two numbers
		average = (num1 + num2)/NUMBER;
		
		System.out.printf("\nThe average of %f and %f is %f.\n", 
			num1, num2, average);
	}
}