   import java.util.Scanner;
   import java.io.*; 

/***********************************************
 * This class will give examples on how to catch
 * various exceptions.
 *
 * @author -Danny Ziemer
 * @version - V1 - 1/10/2007
 * Lab3 
 * Section - 1
 **********************************************/

    public class Exceptions_Lab_ziemerdp_Exceptions
   {
   /********************************************
    * This method will run 5 exceptions and will
    * catch and return an appropriate message when 
    * the user enters an input he or she is not 
    * allowed to enter.
    *
    *	-ArrayIndexOutofBoundsException
    * -FileNotFoundException
    * -Number Format Exception
    * -ArithmeticException
    * -NegativeArraySizeException
    *
    * @param args - unused
    *********************************************/
       public static void main(String args[])
      {
      	//Declarations
         int number;					//holds all integer input values
         int [] array1, array2;	//arrays used to make array exceptions
         String fileName, numString;	//holds Strings to be looked up or converted
         Scanner fileScanner;		//scans the file
         Scanner myScan;			//scans for user input
      
      	//Instantiation
         myScan = new Scanner(System.in);	//creates myScan
      	
      	//creating array1
         array1 = new int[3];
         for(int ii = 0; ii < 3; ii++)
            array1[ii] = ii;
      	
      	//FileNotFoundException
         System.out.println ("Please enter name of file: "); 
         fileName = myScan.nextLine(); //gets file to open
         System.out.println ("The name of the file you want to open is " + fileName); 
      	
         try
         {
            fileScanner = new Scanner (new File (fileName)); //creates fileScanner
            while (fileScanner.hasNextInt()) //looks at the file
            { 
               number = fileScanner.nextInt(); 
               System.out.println (" The value you got from the file is " + number); 
            } 
         }
             catch (FileNotFoundException fnfe)//catches FNFE
            {
               System.out.println("Could not find the file.");
            }
      	
      	//ArrayIndexOutofBoundsException
         System.out.println("How many elements do you want to view in array1(max:3): ");
         number = myScan.nextInt();
         System.out.println("You want to see " + number + " elements.");
         try
         {
            for(int ii = 0; ii < number; ii++)//displays the elements in array1
               System.out.println(array1[ii]);
         }
             catch (ArrayIndexOutOfBoundsException aioobe)//catches AIOOBE
            {
               System.out.println("You cannot view " + number + " elements.");
            }
      	
      	//clear buffer
         myScan.nextLine();   	
      	
      	//NumberFormatException
         try
         {
            System.out.println("Enter a String of numbers that you want to convert into one number(not letters): ");
            numString = myScan.nextLine();//gets the number to be converted
            System.out.println("You want to convert " + numString + " to an integer.");
            System.out.println(Integer.parseInt(numString) + " is the integer.");
         }
             catch(NumberFormatException nfe)//catches NFE
            {
               System.out.println("Cannot convert this into a number.");
            }
      	
      	//ArithmeticException
         try
         {
            System.out.println("What number do you want to divide 10 by(not 0): ");
            number = myScan.nextInt();//gets the value that will be the divisor
            System.out.println("You want to divide 10 by " + number);
            number = 10 / number;//calculates the result
            System.out.println(number);
         }
             catch(ArithmeticException ae)//catches AE
            {
               System.out.println("You cannot divide by 0.");
            }
      	
      	//NegativeArraySizeException
         try
         {
            System.out.println("How many elements do you want in your array(not negative): ");
            number = myScan.nextInt();//gets the array2 size
            System.out.println("You want " + number + " elements.");
            array2 = new int[number];//creates array2
            System.out.println("Array2 has been created with " + number + " elements.");
         }
             catch(NegativeArraySizeException nase)//catches NASE
            {
               System.out.println("You cannot have a negative element size.");
            }
         
      	//Ending message
         System.out.println();
         System.out.println("This program has exitted normally.");
      }
   }
