import java.util.Scanner;
import java.io.*;
import java.util.*;
/**********************************************************************
 * Class purpose: This program shows 5 exceptions that may occur in java
 * and handles them each with try-catch blocks.
 *
 * @author David Petri
 * @version V1 1/17/07
 * Lab 3
 * Section 2
 *********************************************************************/

public class Exceptions_Lab_petridw_ExceptionsLab
{

   /******************************************************************
   * Method purpose: Main method for a program that exemplifies methods
	* of handling exceptions.
   *
   * @param args unused
    *****************************************************************/
	public static void main(String args []) 
	{
		Scanner myScan;
		Object anObject;
		int int1, int2, int3;
		int[] intArray;
		String badInput;
		boolean inputSuccess;
		char aChar;
		final int CHAR_LOCATION = 20;
		
		myScan = new Scanner(System.in);
		intArray = new int[5];
		
		inputSuccess = false;
		
		//Catch an InputMismatchException and repeat the loop until a valid # is entered
		while (!inputSuccess)
		{
			try
			{
				//Get a value from the user and attempt to save it
				System.out.print("Please enter an integer value: ");
				int1 = myScan.nextInt();
				System.out.println("You entered: " + int1);  //echo
				inputSuccess = true;  //Loop ends if the try successfully gets this far
			}
			//Catch the likely InputMismatchException error
			catch (InputMismatchException ime)
			{
				badInput = myScan.nextLine();
				System.out.println("The value '" + badInput + "' is not an int value.");
			}
		}
		inputSuccess = false;
		System.out.println();

		//Attempt to print an element of an array that does not exist
		try
		{
			//This for loop has 6 stages, while the array intArray only has 5 elements
			for (int ii = 0; ii <= 5; ii++)
			{
				System.out.println(intArray[ii]);
			}
		}
		//when the loop above attempts to print the 6th element of the array, it will be
		//caught by the following catch statement
		catch (ArrayIndexOutOfBoundsException aiobe)
		{
			System.out.println("This location is outside the index of the array");
		}
		System.out.println();
		
		//Catch a divide by 0 error and repeat the loop until valid #'s are entered
		//Also checks for InputMismatchExceptions
		while (!inputSuccess)
		{
			try
			{
				//Take 2 #s from the user and attempt to save them
				System.out.print("Enter two integers separated by a space: ");
				int1 = myScan.nextInt();
				int2 = myScan.nextInt();
				//Echo the input
				System.out.println("The two integers are '" + int1 + "' and '" + int2 + "'.");
				
				//Attempt the division
				int3 = int1 / int2;
				System.out.println(int1 + " / " + int2 + " = " + int3 + ".");
				
				//If successful thus far, set inputSuccess to true so the loop will end
				inputSuccess = true;
			}
			//Catch the divide by zero error
			catch (ArithmeticException ae)
			{
				System.out.println("Cannot divide by zero.");
			}
			//Catch the InputMismatchException
			catch (InputMismatchException ime)
			{
				badInput = myScan.nextLine();
				System.out.println("The value '" + badInput + "' is not an int value.");
			}
		}
		inputSuccess = false;
		System.out.println();
		
		//Create a StringIndexOutOfBoundsException and catch it
		try
		{
			//This String has 11 characters and thus takes up char slots 0-10.
			badInput = "aaddffegmty";
			
			//Attempt to store a character from the badInput String from spot 20
			aChar = badInput.charAt(CHAR_LOCATION);
		}
		//Catch the StringIndexOutOfBoundsException
		catch(StringIndexOutOfBoundsException siobe)
		{
			System.out.println("Position '" + CHAR_LOCATION + "' does not exist in the String.");
		}
		System.out.println();
		
		//Handle a ClassCastException
		try
		{
			//Instantiate an Integer object
			anObject = new Integer(0);
			//Print a line attempting to cast the Integer as a String
			System.out.println((String)anObject);
			
		}
		catch(ClassCastException cce)
		{
			System.out.println("Cannot cast an Integer into String.");
		}
		System.out.println();
		
		System.out.println("The program will now end normally.");
	}
}	
		