/**********************************************
 *
 * @author	Ryan Decker
 * @date		January 17, 2007
 * @version	1
 * 
 * This program will try and catch five exceptions
 * that may occur while the program is running.
 *
 */
 
import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.*;

public class Exceptions
{

	/*
	 * The main constructor in the class.  Takes in
	 * arguments and implements them as part of the
	 * code.
	 * @return void - returns nothing
	 */
	public static void main (String [] args)
	{
		
		int num1, num2; // these numbers will be divided
		int result; // the result of num1/num2
		int[] array1;
		int arrayInit; // how long the array is going to be
		String fileName;
		String word;
		
		FileReader freader; // for file input
		BufferedReader inputFile; // for file input
		Scanner keyboard;
		
		// initializes the Scanner "keyboard."
		keyboard = new Scanner(System.in);
				
		// message welcomes the user to the program
		System.out.println("Welcome, this is the "
			+ "CS239 exception handler.\n");
		// prompts user for a number for num1
		System.out.print("Please enter a number: ");
		num1 = keyboard.nextInt();
		System.out.print("\nPlease enter another number: ");
		num2 = keyboard.nextInt();
		
		// echos the input
		System.out.println("\nYou entered " + num1
			+ " and " + num2);
		
		// does the dividing, will catch if divided by 0	
		try
		{
			result = num1/num2;
			System.out.println(num1 + " / "
				+ num2 + " = " + result + "\n");
		}
		catch (ArithmeticException ae)
		{
			System.out.println("\nThe following error occured: "
				+ ae.getMessage());
			System.out.println("Please don't divide by zero.");
		}
		
		// catches if the user trys a negative number for the array length
		try
		{
			System.out.print("\nHow many numbers would you like "
				+ "to store in the array? ");
			arrayInit = keyboard.nextInt();
			System.out.println("\nThe array can hold " + arrayInit
				+ " numbers");
		}
		catch (NegativeArraySizeException nase)
		{
			System.out.println("\nThe following error occured: "
				+ nase.getMessage());
			System.out.println("Please use positive numbers.");
			System.out.println("The array will be set to 3 by default");
			arrayInit = 3;
		}
			
		// initializes the integer array
		array1 = new int[arrayInit];
		
		// stores information into an array, catches if wrong data type
		try
		{
			for (int x = 0; x < arrayInit; x++)
			{
				System.out.print("\nEnter an integer to store: ");
				array1[x] = keyboard.nextInt();
			}
			System.out.print("\nThe array contains ");
			for (int x = 0; x < arrayInit; x++)
			{
				System.out.print(array1[x] + " ");
			}
		}
		catch (InputMismatchException ime)
		{
			System.out.println("\nThe following error occured: "
				+ ime.getMessage());
			System.out.println("Please make sure you're only "
				+ "entering integers for the array.");
		}
		
		System.out.print("\nEnter a file you'd like to find: ");
		fileName = keyboard.next();
		System.out.println("\nWill try to find " + fileName);
		
		// will attempt to find the file
		try
		{
			freader = new FileReader(fileName);
			inputFile = new BufferedReader(freader);
			System.out.println("\nThe file was found");
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println("\nThe following error occured: "
				+ fnfe.getMessage());
			System.out.println("The file could not be found.");
		}
		
		System.out.print("\nInput something to parse to an integer: ");
		word = keyboard.next();
		System.out.print("\n" + word + " will be parsed");
		
		// will try to parse the input
		try
		{
			num1 = Integer.parseInt(word);
			System.out.println("\n" + num1 + " is now an integer");
		}
		catch (NumberFormatException nfe)
		{
			System.out.println("\nThe following error occured: "
				+nfe.getMessage());
			System.out.println("Can't parse Strings.");
		}
		
		System.out.println("\nProgram has ended successfully.");
	}// end of main
}// end of program