import java.util.Scanner;
import java.io.*;
/*********************************************************
*	This program will run and excute try/catch blocks to
*	help understand the use of exception handlers
*
*
*	@author - Joseph Claus
*	@version - V1
*
*/
// Date: 1/17/07
// Section: 1
// Lab: 3
public class Exceptions_Lab_clausjs_ExceptionHandler
{
	/*************************************
	*	This is the main method
	*
	*
	*	FileNotFoundException, ArithmeticException, ArrayIndexOutOfBoundsException, NumberFormatException
	*
	*/
	
	public static void main(String[] args)
	{
		Scanner keyboard;
		int num;
		int error;
		Scanner fileScanner;
		String fileName;
		int value;
		int name;
		int whatever;
		int x;
		String Input;
		
		String[] errors = new String[3];
		
		keyboard = new Scanner(System.in); //Initializes new Scanner object
		
		System.out.print("Enter a number of any value: "); //Takes in a value to divide by zero which will cause an ArithmaticException
		num = keyboard.nextInt();
		System.out.println();
		System.out.println(num); //Echos input of num variable
		
		try
		{
			error = num / 0; //This will try and divide the number inputted by the user by zero
		}
		catch (ArithmeticException ae) //This will catch the exception and give a message
		{
			System.out.print("It is impossible to divide any number by zero.");
			System.out.println();
		}
		
		try
		{
			for (int index = 0; index <= 4; index++) //This loop tries to run through an array greater than the one it is using
			{
				System.out.print(errors[index]);
				System.out.println("\n");
			}
		}
		catch (ArrayIndexOutOfBoundsException aioobe) //The exception is handled here
		{
			System.out.print("The array is not that long, it has caused an error - moving on");
			System.out.println("\n");
		}
		
		System.out.print("Please enter a file name to be opened: "); //Takes in a file name
		fileName = keyboard.next();
		System.out.println();
		
		System.out.println("The file you wanna open is " + fileName); //Echos input
		System.out.println();
		
		try
		{
			fileScanner = new Scanner(new File (fileName));
			while (fileScanner.hasNextInt()); //will attempt to find the file
			{
				value = fileScanner.nextInt();
			}
		}
		catch (FileNotFoundException fnfe) //Will handle the exception when file is not found
		{
			System.out.print("File cannot be found. Try agian later.");
			System.out.println("\n");
		}
		
		try
 	   {
    	  System.out.print("User...enter a word of some kind: "); //Asks user for input of a word
		  Input = keyboard.next();
		  System.out.println("\n");    
		  x = Integer.parseInt(Input); //Attempts to change the word into an integer
    	}
    	catch (NumberFormatException nfe) //handles the exception when java realizes it cannot change into an integer
    	{
        System.out.println(nfe.getMessage() + " is not a valid format for an integer." ); //exception error message
    	}
		
		System.out.println("This program has ended normally");
		
	}	
}