import java.util.*;
import java.io.*;

/***********************************************
 * This program creates and handles 5 different exceptions
 *
 * @author Geoff Wellington
 * @version 1
 * @exception FileNotFoundException
 * @exception InputMismatchException
 * @exception NumberFormatException
 * @exception ArrayIndexOutOfBoundsException
 * @exception ArithmeticException
 ************************************************/	
public class Exceptions_Lab_wellingm_exs
{
	public static void main (String [] args)
	{	
						      
		int wrong, other;
		int [] hold;
		
		other = 0;
		String bad, file;
		bad = "";			
		Scanner in;
		Scanner fileScanner;
		
		in = new Scanner (System.in);
					
		System.out.print("Please enter filename to open: ");
		file = in.nextLine();
		System.out.println();
		
		//this is the FileNotFound exception
		try
		{
			fileScanner = new Scanner (new File (file));
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println("File was not found.");
			System.out.println();
			
		}
		
		//this is the InputMismatch exception			
		try
		{
			System.out.print("Enter an int (but actually enter a dec so i can catch the error): ");
			wrong = in.nextInt();
			System.out.println();
			
		}
		catch (InputMismatchException ime)
		{
			System.out.println("Incorrect input format.");
			System.out.println();
		}
		
		//this is the NumberFormat exception
		try
		{
			in.nextLine();
			System.out.print("Enter a string to convert to an int: ");
			bad = in.nextLine();
			System.out.println();
			
			other = Integer.parseInt(bad);
		}
		catch (NumberFormatException nfe)
		{
			System.out.println("Cannot convert this string to int.");
			System.out.println();
		}
		
		
		//this is the ArrayIndexOutOfBounds exception
		try
		{		     
			System.out.print("Enter a number: ");
			wrong = in.nextInt();
			System.out.println();
			hold = new int[wrong];
							
			for (int i=0; i <= wrong; i++)
			{
				hold [i] = 0;
			}
		}
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("The array index went out of bounds.");
			System.out.println();
		}
		
		//this is the Arithmetic exception
		try
		{
			System.out.print("Enter a number to divide 10 by: ");
			wrong = in.nextInt();
			System.out.println();
			
			//to create the error, the user enters a 0
			wrong = 10 / wrong;
		}
		catch (ArithmeticException ae)
		{
			System.out.println("Arithmetic exception.");
		}	
	
	}
}