 import java.util.Scanner;
 import java.util.*;
 import java.io.*;
/*******************************************************************
 Name: Trevor Spalt
 Date: January 17, 2007
 Section: 2
 Lab: 3
/*******************************************************************
 * This lab is going through 5 different exceptions
 * and is trying to handle each one of these exceptions.
 *	I am going to handle the following exceptions:
 * 1. An array that is out of bounds
 * 2. A mismatch of input data
 * 3. Arithmetic Exception (user divides by 0)
 * 4. FileNotFound Exception
 * 5. 
 */
 	
    public class Exceptions_Lab_spaltts_Exceptionsb
   {
      public static void main (String args[])
      {
      	int[] array = {1,2,3}; //Create an Array
			int lengthOfArray; // Create a length variable
			Scanner keyboard; // Make a keyboard scanner
			keyboard = new Scanner(System.in);
			
			// This block makes an array go out of its index
			// and then catches it once it leaves the index
			try
			{
				// Created a for loop to go one outside of the index
				// However it still prints out the contents of the loop
				// until it actually leaves the index
				for(int counter = 0; counter <= 3; counter++)
				{
					System.out.println("The number in index " + counter + " is " + array[counter]);
				}
			}
			catch(ArrayIndexOutOfBoundsException aioobe)
			{
				// Output to the user that the index is out of bounds
				System.out.println("The array index is out of bounds");
			}
			
			// Print out a few blank lines between the blocks
			System.out.println("\n");
			
			// This block will catch the exception of a user inputing
			// a value that is not of the correct type
			
			int number; // This is the value that will be filled with
			            // the wrong value
			
			try
			{
				// Tell the user to enter a number	
				System.out.print("Enter a number of integer value: ");
				number = keyboard.nextInt();
				
				// Have the user reEnter a number
					System.out.print("\nReEnter the number: ");
					
				// Create a loop that echos the value that
				// the user has given if it is indeed correct
				if(keyboard.hasNextInt())
				{					
					System.out.println("The number that you entered was: " + number);	
				}
				
				// Use this to clear the keyboard
				keyboard.nextLine();
			}
			catch(InputMismatchException ime)
			{
				System.out.println(" The value that you have entered " +
										"is not of the same type that is expected");
			}
			
			// Print out a few blank lines between the blocks
			System.out.println("\n");
			
			// Use this to clear the keyboard
			keyboard.nextLine();
			
			// This method takes in two integers from the user and
			// divides the first one by the second one. If the user
			// enters a 0 for the second number it will catch the error
			
			double num1, num2; // Create the two integer values
			double finalNumber; // Create a variable to hold final number
			
			// Output a message telling the user to input the values
			System.out.print("Please enter the first number: ");
			num1 = keyboard.nextInt();
			System.out.print("\nPlease enter the second number: ");
			num2 = keyboard.nextInt();
			
			// Echo the numbers given
			System.out.println("You entered both " + num1 + " and " + num2);
			
			try
			{
				if(num2 != 0)
				{
					finalNumber = num1/num2;
					System.out.println(num1 + " / " + num2 + " = " + finalNumber);
				}
			}
			catch(ArithmeticException ae)
			{
				System.out.println("You entered a number to make an Arithmetic Error");
         }
			
			// Print out a few blank lines between the blocks
			System.out.println("\n");
			
			// Use this to clear the keyboard
			keyboard.nextLine();
			
			// This block asks the user for a file name. If the file name is correct the block
			// will open what is within that file. However, if it is incorrect, the computer
			// will disregard what is in that file.
			
			String fileName; // Delcare variable to hold the file name
         Scanner fileScanner; // Delare scanner to hold the file information
         int value; // Declare a value to hold the integers for the file
      
			// Ask the user to input the filename
         System.out.println (" Please enter name of file holding integers and hit return ");
         fileName = keyboard.nextLine();
      
			// Echo out the filename
         System.out.println (" The name of the file you want to open is " + fileName);
         
         try
		 	{ 
				// make file name a new scanner object
		      fileScanner = new Scanner(new File (fileName));
				
				while (fileScanner.hasNextInt()) 
         	{
               value = fileScanner.nextInt();
               System.out.println (" The value you got from the file is " + value);
         	}  
			}
         catch (FileNotFoundException fnfe)
			{
	  			System.out.println (" The file you wanted to open was not found ");
			}
			
			// Print out a few blank lines between the blocks
			System.out.println("\n");
			
			// This block exception catches a parsed integer that is parsing a string.
			// If a string is being parsed then it will catch the error and tell the user/\.
			
         String str = "abcde"; // Declare a string variable
			int parseNumber; // Declare a number variable to hold the parsed int
						
			try
			{
				parseNumber = Integer.parseInt(str); // Tries to parse the string
			}
			catch(NumberFormatException nfe)
			{
				System.out.println("You are not parsing an Integer");
			}
			
      }
   }