   import java.util.*;
   import java.io.*;
/***********************************************
 * Tests for and catches five different exceptions.
 *
 * @author - Michael Siltz
 * @version 1
 ************************************************/
// Date: January 17, 2006
// Section 1
// Lab 3
    public class Exceptions_Lab_siltzma_Exception
   {
   	/********************************************
   	 * Tests for and catches five different exceptions:
   	 * the InputMismatchException, the Arithmetic Exception,
   	 * the NegativeArraySizeException, the ArrayIndexOutOfBoundsException,
   	 * and the FineNotFoundException.
   	 *
   	 * @param args unused
   	 *********************************************/    
       public static void main(String [] args)
      {
         int[] num, array;
         int value, elementNum, divisor, arrayLength;
         double newNum;
         String name, errorString;
         boolean continueLoop; // Continues the do-while loops if an exception is triggered
         Scanner keyboard;
         FileReader freader;
      
         keyboard = new Scanner(System.in);      
         continueLoop = false; // Initializes boolean variable
         arrayLength = 0; // Initializes the interger variable
         value = 0; // Same as above
         num = new int[0]; // Initializes the array
   		   	
			
			
			/*************************
      	* InputMismatchException
			*************************/
         do
         {
            System.out.print("Please enter a number to be multiplied by 100: ");
            try
            {
               newNum = keyboard.nextDouble();
               System.out.println(newNum + " times 100 equals " + (newNum * 100)); // Echoes the input and the result
               continueLoop = false; // Ends the loop by changing the boolean value to false
            }
      			// Reads in the improper input and echoes it to the user, followed by the reason          
					 catch (InputMismatchException ime)
               {
                  errorString = keyboard.nextLine();
                  System.out.println(errorString + " is not a number."); 
                  System.out.println("InputMismatchException generated.");
                  continueLoop = true; // Continues the loop by changing the boolean value to true  
               }
         } while (continueLoop);
      	
      	
      	
			/**********************
			* ArithmeticException
			**********************/
         System.out.println();     
         do
         {         
            System.out.print("Please enter an integer (other than zero) to divide 100 by: ");        
            try
            {			
               divisor = keyboard.nextInt();        
               System.out.println("100 divided by " + divisor + " equals " + (100 / divisor)); // Echoes the input 
																															  // and prints the result
               continueLoop = false;
            }
      			// Same as above          
					 catch (InputMismatchException ime)
               {
                  errorString = keyboard.nextLine();
                  System.out.println(errorString + " is not an integer.");
                  System.out.println("InputMismatchException generated.");
                  continueLoop = true;        
               }
              	// Prints that the user cannot divide by zero and the exception generated
					 catch (ArithmeticException ae)
               {
                  System.out.println("Cannot divide by zero.");
                  System.out.println("ArithmeticException generated.");
                  continueLoop = true;
               }
         } while (continueLoop);
      	
      	
      	
			/*****************************
			* NegativeArraySizeException
			*****************************/
         System.out.println();
         do
         {
            System.out.print("Please enter the length of an array: ");
            try
            {      
               arrayLength = keyboard.nextInt();
               array = new int[arrayLength]; // Initializes the array
               System.out.println("The array has " + arrayLength + " elements."); // Echoes the length of the array
               continueLoop = false;
            }
               // Same as above
					 catch (InputMismatchException ime)
               {
                  errorString = keyboard.nextLine();
                  System.out.println(errorString + " is not an integer.");
                  System.out.println("InputMismatchException generated.");
                  continueLoop = true;        
               }
					// If the integer is negative, it prints the negative integer and the reason for the error,
					// followed by the specific exception
                catch (NegativeArraySizeException nase)
               {
                  System.out.println(arrayLength + " is not an acceptable length as an array cannot be negative.");
                  System.out.println("NegativeArraySizeException generated.");
                  continueLoop = true;
               }
         } while (continueLoop);
      	
      	
			
      	/*********************************
			* ArrayIndexOutOfBoundsException
			*********************************/
         System.out.println();
         do
         {
            System.out.print("Please enter the length of another array: ");
            try
            {        
               value = keyboard.nextInt();
               num = new int[value];        
               for (int elementVal = 0; elementVal < value; elementVal++)
               {
                  num[elementVal] = elementVal; // Provides each element with a value
               }
            }
					// Same as above
                catch (InputMismatchException ime)
               {
                  errorString = keyboard.nextLine();
                  System.out.println(errorString + " is not an integer.");
                  System.out.println("InputMismatchException generated.");
                  continueLoop = true;        
               }
					// Same as above       
                catch (NegativeArraySizeException nase)
               {
                  System.out.println(value + " is not an acceptable length as an array cannot be negative.");
                  System.out.println("NegativeArraySizeException generated.");
                  continueLoop = true;
               }
         } while (continueLoop);
         do
         {
				// Echoes the array's length and prompts for the number of elements to be displayed
            System.out.print("You want an array that has " + value 
               + " elements. How many elements do you want to display? ");  
            try
            {
               elementNum = keyboard.nextInt();
               System.out.println(elementNum + " elements will be displayed."); // Echoes the number to be displayed           
     				// Prints each element up to the number of elements requested to be displayed         
					for (int counter = 0; counter < elementNum; counter++)
               {            
                  System.out.println("Element " + (counter + 1) + ": " + num[counter]);
                  continueLoop = false;
               }
            }
      			// Same as above          
					 catch (InputMismatchException ime)
               {
                  errorString = keyboard.nextLine();
                  System.out.println(errorString + " is not an integer.");
                  System.out.println("InputMismatchException generated.");
                  continueLoop = true;        
               }
					// After it prints the values of all the elements, if there are elements out of range of the
					// array, it specifies that it can only display and certain number of elements and outputs the exception           				 
                catch (ArrayIndexOutOfBoundsException aioobe)
               {
                  System.out.println("Cannot display more than " + value + " elements.");
                  System.out.println("ArrayIndexOutOfBoundsException generated.");        
                  continueLoop = true;          
               }
         } while (continueLoop);
      	
      	
			
      	/************************
			* FileNotFoundException
			************************/
         keyboard.nextLine(); // Catches the new line generated by nextInt()
         System.out.println();      
         do
         {
            System.out.print("Please enter the name of a file: ");
            name = keyboard.nextLine();
            try
            {
               freader = new FileReader(name);
               System.out.println(name + " was found."); // Prints (and echoes) that the file was found
               continueLoop = false;
            }
					// If file is not found, it echoes the file name and specifies that it was not found
					// before printing the exception generated.
                catch (FileNotFoundException fnfe)
               {
                  System.out.println(name + " was not found.");
                  System.out.println("FineNotFoundException generated.");
                  continueLoop = true;
               }
         } while (continueLoop);
      	
      	
      	
         System.out.println("\nThis program has ended normally."); // Prints successful completion message	    
      }
   }