   import java.io.*;
   import java.util.*;

/**************************************
 * ExceptionHandling.java - This program will handle 5 different exceptions
 * including: InputMismatchException, ArithmeticException, ArrayIndexOutOfBoundsException,
 * FileNotFoundException, and NullPointerException.
 *
 * @author Chris MacDonald
 * @version 1.1 
 **************************************/
// Date:  January 17, 2007
// Section 1
// Lab 2

public class Exceptions_Lab_macdoncj_ExceptionHandling
{
	public static void main(String[] args)
   {	
  		// Declarations
      boolean temp[];
      int num1;
		int num2; 
		int numerator;
		int denominator;
		double result;
		File inFile;
		Scanner sc;
		String fileName;
		String nullString;
           
      // Handling an InputMismatchException
		System.out.println("Please enter 2 integers.");
		         
      try
      {
			// Instantiate a new Scanner for this try-catch block
			sc = new Scanner(System.in);
         
			System.out.print("Integer 1: ");
         num1 = sc.nextInt();
         System.out.print("Integer 2: ");
         num2 = sc.nextInt();
   
			// Echo user input      
			System.out.println("You entered " + num1 + " and " + num2 + " as your integers.");
			
			System.out.println("Input is correct.");
      }
      
		catch(InputMismatchException ime)
      {
      	System.out.println("Error! Input not correct!");
      }
      	
      // Handling an ArithmeticException and an InputMismatchException
		System.out.println("\nPlease enter 2 numbers to be divided.");
		
      try
      {
			// Instantiate a new Scanner for this try-catch block
			sc = new Scanner(System.in);
			
         System.out.print("Integer 1: ");
         numerator = sc.nextInt();
         System.out.print("Integer 2: ");
         denominator = sc.nextInt();
			
			// Echo user's input
			System.out.println("You entered " + numerator + " and " + denominator + " as your numbers.");
			
			result = (numerator / denominator);
         System.out.println(numerator + " / " + denominator + " = " + result);
      }
      
		catch(InputMismatchException ime)
      {
      	System.out.println("Error! Input not correct!");
      }   
      
		catch(ArithmeticException ae)
      {
      	System.out.println("Error! Cannot divide by 0!");
      }
      
		// Handling an ArrayIndexOutOfBoundsException
		temp = new boolean[6];
		
		try
		{
			System.out.println("\nStarting the for-loop...");
			
			for(int i = 0; i <= 6; i++)
			{
				if(i % 2 == 1)
				{
					temp[i] = true;
					System.out.println("True");
				}
				else
				{
					temp[i] = false;
					System.out.println("False");
				}
			}
		}
		
      catch(ArrayIndexOutOfBoundsException aioobe)
      {
      	System.out.println("Error! Array does not exist!");
      }
  
  		// Handling a FileNotFoundException
		// Set to null to avoid "variable might not have been initialized" error    
		inFile = null;
		fileName = null;
		
		try
		{	
			// Instantiate a new Scanner for this try-catch block
			sc = new Scanner(System.in);
			
			System.out.print("\nEnter the file name: ");
			fileName = sc.nextLine();
			
			inFile = new File(fileName);
			sc = new Scanner(inFile);
		}
   
		catch (FileNotFoundException fnfe)
      {
			System.out.println("File name you entered: " + fileName);
      	System.out.println("Input file " + fileName + " does not exist!");
      }
		
		// Handling a NullPointerException
		// This string is deliberately set to null to cause the exception
		nullString = null;
  
    	try 
		{
      	int length = nullString.length();
         String s = new String(nullString);
    	}
		
		catch(NullPointerException npe)
      {
      	System.out.println("\nError! The string's length is null.");
      }
		
		System.out.println("\nThis program has ended normally.");
	}
}