   import java.util.Scanner;
/***********************************************
* @author - Matthew Bernardo 
* @version - V1 - January 17, 2007
* Lab 3
* Section 1
************************************************/
/*
/ References and Ackknowledgements: I have recieved 
/ no help on this programming assignment
/
/***********************************************/
    public class Exceptions_Lab_bernarmj_Exception 
   {
   /********************************************
   * This method creates five exceptions and five catch
   * blocks to handle the exceptions. in this order:
   * 1. ArrayIndexOutOfBoundsException
   * 2. NegativeArraySizeException
   * 3. ArithmeticException: / by zero
   * 4. NumberFormatException
   * 5. NullPointerException
   *********************************************/
       public static void main(String args[])
      {
         int [] array;        //array for the ArrayIndexOutOfBoundsException
         int [] array2;       //array for the NegativeArraySizeException
         int index;           //array index
         int num;             //negative index 
         int length;          //index + 1
         int divide;          //number to be divided
         int falseLength;     //int for the .length() of falsestr
         double divided;      //divided number
         String str = "" ;    //string for NumberFormatException
         String falsestr = "";// string to be null
         Scanner input;       //scanner
         
         input = new Scanner(System.in);
      
      	
      /********************************************
      * ArrayIndexOutOfBoundsException "block"   
      * This creates an array with the given index 
      * and tries to store one more number than it has indexes
      *********************************************/
      
         try                                                   //ArrayIndexOutOfBoundsException try block
         {
            System.out.print( "Give an index for an Array: "); //asking for index
            index = input.nextInt();
            length = (index + 1);
            System.out.print("\n You entered " + index + "\nan array will be created with an index of "
               + index + "and will try to store one more number that the index.\n");
            array = new int[index];
            for( int ii = 0; ii <= length; ii++)               //for loop, sets the values of the indexes (index +1) times.
            {
               array[ii] = ii;
               System.out.print(array[ii] + " is the number stored in array index " + ii);
               System.out.print("\n");
            }
         }
         
             catch (ArrayIndexOutOfBoundsException aioobe)     //ArrayIndexOutOfBoundsException catch block
            { 
               System.out.print( "\nArrayIndexOutOfBoundsException! its trying to "
                  + " store one more number than the array has indexs!\n");
            }
      	
      /********************************************
      * NegativeArraySizeException "block"   
      * This creates an array with the given negative index 
      * 
      *********************************************/
         try                                                   //NegativeArraySizeException try block
         {
            System.out.print("\n\nEnter a negative number: "); //getting a negative number
            num = input.nextInt();
            array2 = new int[num];
         }
             catch(NegativeArraySizeException nase)            //NegativeArraySizeException catch block
            {
               System.out.print("\nNegativeArraySizeException! You cant make an array with a negative size!\n"); 
            }
      
      
      /********************************************
      * ArithmeticException: / by zero "block"   
      * This eventually divides a number by zero 
      * 
      *********************************************/
         try                                             //ArithmeticException: / by zero try block
         {
            System.out.print("\n\n Select a number: " ); //getting a number to divide
            divide = input.nextInt();
            System.out.print("You picked " + divide + " divide will now be divided by "
               + (divide + 1) + " numbers.\n\n");
         
            for(int ii = divide; divide >= 0; ii--)      //dividing the number
            {
               System.out.print(divide + " / " + ii + " = ");
               divided = divide / ii;
               System.out.print(divided + "\n");
            }
         }
         
             catch(ArithmeticException ae)               //ArithmeticException: / by zero catch block
            {
               System.out.print("You can not divide by zero!\nArithmeticException!\n" );
            }
        
      /********************************************
      * NumberFormatException "block"   
      * This tries to parseInt() a String of letters 
      * 
      *********************************************/
         try                                                //NumberFormatException try block
         {
            System.out.print("\nPlease enter a string " 
               + "(add letters to cause the excpetion): "); //getting a string with letters
            input.nextLine();
            str = input.nextLine(); 
            System.out.print("You entered " + str + "\n" );
            length = Integer.parseInt(str);                 //causing the error
            System.out.print("The length is " + length);
         }
         
             catch(NumberFormatException nfe)               //NumberFormatException catch block
            {
               System.out.print("NumberFormatException! " + str + " cant be formatted with parseInt! ");
            }
      
      /********************************************
      * NullPointerException "block"   
      * This tries to .length() a null String  
      * 
      *********************************************/
         try                                              //NullPointerException try block
         {
            System.out.print("\n\nEnter a String: ");     //getting a string
         
            falsestr = input.nextLine();
            System.out.print("\nYou entered" + falsestr + "\n" + falsestr 
               + " will now be set to null and the length of the string taken.\n");
            falsestr = null;                              //setting the string to null
            falseLength = falsestr.length();              //trying to .length() the null string
            System.out.print("The length of your string is " + falsestr + "\n");
         }
         
             catch(NullPointerException npt)              //NullPointerException catch block
            {
               System.out.print("\nNullPointerException!\nyou cant .length() a null string!");
            }     
         
         finally
         {
            System.out.print("\n\nAll five exceptions executed and handled!");   //ending statement
         }
      
      }
   } 
