import java.util.Scanner;
import java.io.*;
/*******************************************************************
 * This program deals with several different exceptions and handles each
 * of the them.  In this program I used NumberFormatException, 
 * ArrayIndexOutOfBoundsException, FileNotFoundException, ArithmeticException,
 * NegativeArraySizeException.
 *
 * @author - Trevor Caudill 
 * @version - V1 - 1/17/07
 * Lab time: MW 1:25-2:15
 * Section 1
 * Lab 3
 *****************************************************************/
public class Exceptions_Lab_caudiltt_Exceptions
{
	public static void main (String [] args)
	{
	
	
// First Exception: NumberFormatException

		String stringToNum;
		int number;
		
		stringToNum = "BadString";
		
		// takes a string and converts it to an int value.
		try
		{
			number = Integer.parseInt(stringToNum);
		}
		
		// echos the string and prints the NumberFormatexception message
		catch(NumberFormatException nfe)
		{
			System.out.println("Error: stringToNum could not be converted to " +
				"an int for the string \"" + stringToNum + "\"\n");
		}// end of first exception.
		
		
// Second Exception: ArrayIndexOutOfBoundsException
		
		// sets up an array of 5 elements.
		int[] numberArray = {1, 2, 3, 4, 5};
		
		// uses a for loop and prints out each element of the array
		try
		{
			for(int i = 0; i <= 5; i++)
				System.out.println(numberArray[i]); 
		}
		
		// tells user the length of their array and prints the ArrayIndexOutOfBoundsException message.
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("\nError: Array index out of bounds." +
				" Your array only has " + numberArray.length + " elements.");
		}// end of second exception.
		
		
// Third Exception:  FileNotFoundException

		String fileName;
		Scanner fileScanner;
		
		fileName = "bad.txt";
		
		// finds a file and prints the int values from it.		
		try
		{ 
		 	fileScanner = new Scanner (new File (fileName)); 
	   } 
		
		 // prints out the file name and fileNotFoundException message.
		 catch (FileNotFoundException fnfe) 
		 { 
		 	System.out.println ("\n\nError: The file named " + fileName + " was not found ");
		 } // end of third exception.
		 
		 
// Fourth Exception: ArithmeticException

		int num1, num2, result;
		
		// initialize variables
		num1 = 6;
		num2 = 3;
		result = 0;
		
		// divides num1 by num2 and prints result.  Then divides num1 by zero and prints result.
		try
		{
			result = num1 / num2;
			System.out.println("\nResult = " + result);
			
			result = num1 / 0;
			System.out.println("\nResult = " + result);
		}
		
		// prints out the arithmeticException message.
		catch (ArithmeticException ae)
		{
			System.out.println("\nError: You cannot divide a number by zero.");
		}// end of fourth exception.

				
// Fifth Exception:  NegativeArraySizeException
		
		final int ARRAY_SIZE = -10;
		
		// assigns a negative size value to an integer array.
		try
		{
			int[] negativeArray = new int[ARRAY_SIZE];
		}
		
		// echos ARRAY_SIZE and prints NegativeArraySizeException message.
		catch (NegativeArraySizeException nase)
		{
			System.out.println("\nError:  You cannot have an array of negative size. Your " +
				"array size was: " + ARRAY_SIZE);
		}// end of fifth exception.
		
		
		// Informs user the program has ended normally.
		System.out.println("\nThe program has ended normally.");
	
	
	}
}