   import java.util.Scanner;
   import java.util.Random;
   import java.util.InputMismatchException;
/**
 * This is a exception handling program
 * 
 * @author: McCabe Weaver
 * @version: v1
 * January 17th, 2007
 * Section 1
 *
 * This program will handle the following exceptions:
 * 	ArrayIndexOutOfBoundsException
 * 	ArithmeticException
 * 	NullPointerException
 * 	InputMismatchException
 *		StringIndexOutOfBounds
 **/
 /**
 * main method
 *
 * @param unused
 * @return unused
 **/
    public class Exceptions_Lab_weaverrm_ExceptionHandler
   {
       public static void main (String [] args)
      {
         Scanner reader = new Scanner(System.in);
      
      
         System.out.println("This is a Java exception handling program.");
      
      //The following block of code will handle an ArrayIndexOutOfBoundsException
         int[] array1;
         array1 = new int[10];
         int index1 = 0, newValue;
         //Asking for an index to be changed, and a new value to be put into that index
			System.out.print("\nEnter an index to be changed (0-9): ");
         index1 = reader.nextInt();
         System.out.print("\n");
         System.out.print("Enter a new value: ");
         newValue = reader.nextInt();
         System.out.print("\n");
			
         try
         {
            array1[index1] = newValue;
         }
			//Making sure the index is within bounds of the array
         catch (ArrayIndexOutOfBoundsException aioobe)
         {
            System.out.println("Index out of bounds.");
				System.out.println("Given index: " + index1);
				System.out.println("New value: " + newValue);
         }
			
		//The following block of code will handle an ArithmeticException
		   int dividend, divisor, quotient;
			
			//Getting input for simple division
			System.out.print("\n");
      	System.out.print("Enter the dividend: ");
			dividend = reader.nextInt();
			System.out.print("\n");
			System.out.print("Enter the divisor: ");
			divisor = reader.nextInt();
			System.out.print("\n");
         try
			{
			   //Attempting to divide
			   quotient = dividend / divisor;
			}
			catch (ArithmeticException ae)
			{
			   System.out.println("Arithmetic Exception.");
				System.out.println("Dividend: " + dividend);
				System.out.println("Divisor: " + divisor);
				System.out.println("Changing divisor to 1.");
				divisor = 1;
			}
			quotient = dividend / divisor;
			System.out.println("\nQuotient: " + quotient);
			
		//The following block of code will handle a NullPointerException
		   Random[] generator = new Random[10]; //Creating an array of Random objects
		   System.out.println("\nThis is testing a NullPointerException.");
		   
		   try
		   {
		      generator[0].nextInt(); //Pointing to an object that is null
		   }
		   catch (NullPointerException npe)
		   {
		      System.out.println("Null Pointer Exception caught.");
		   }
		   
		//The following block of code will handle an InputMismatchException
		   int test1;
		   
		   try
		   {
		      //Taking an integer input from the user
            System.out.print("\nEnter a valid integer: ");
            test1 = reader.nextInt();
            System.out.println();
		   }
		   catch (InputMismatchException ime)
		   {
		      System.out.println("\nYou have not entered a valid integer.");
		      System.out.println("\nValue changed to 1.");
		      test1 = 1;
		   }
		   System.out.println("\nValue entered: " + test1);
		   
		//The following block of code will handle a StringIndexOutOfBounds exception
		   String myName = "McCabe Weaver";
		   int subIndex;
		   String newStr;
         System.out.print("\nEnter an int value to begin the substring: ");
         subIndex = reader.nextInt();
         System.out.print("\n");
         
		   try
		   {
		      //Attempting to make a substring of the above string
		      newStr = myName.substring(subIndex);
		   }
		   catch (StringIndexOutOfBoundsException sioob)
		   {
		      //Setting the new substring index to 0
		      System.out.println("\nStringIndexOutOfBounds error.");
		      System.out.println("Index entered: " + subIndex);
            System.out.println("String length: " + myName.length());
            System.out.println("Changing index to 0.");
            subIndex = 0;
            newStr = myName.substring(subIndex);
		   }
		   System.out.println("\nSubstring created: " + newStr);
		   System.out.println("\n\n\nTHIS PROGRAM ENDED NORMALLY.");
      }
   }