import java.util.Scanner;
import java.io.*;

/********************************************************************
 * This program gives practice with using java exceptions.  The
 * program uses uses five types of exceptions.
 *
 * The exceptions used are:
 *
 * 1. ArrayIndexOutOfBounds
 * 2. FileNotFound
 * 3. NegativeArraySize
 * 4. NumberFormat
 * 5. Arithmetic
 *
 * @author - Sean Combs
 * @version - Version 1
 *
 *	Date: 1/17/2007
 *	Section 001
 *	Lab 3
*********************************************************************/
 
public class Exceptions_Lab_combssm_Exceptions
{

	public static void main(String args[])
	{
		
	// 1: ArrayIndexOutofBounds
		System.out.println("1. ArrayIndexOutofBounds");
		
		//Declares an array of 5 elements
		int[] numbers = {10, 20, 30, 40, 50};
		
		String numString = "";
		
		try
		{
			//Attempts to display 6 elements for an array of 5 elements
			for(int i=0; i <= 5; i++)
			{
				numString = numString + numbers[i];
			}
		}
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("Error: Array Index Out of Bounds Exception." +
				" Array only has " + numbers.length + " elements.");
		}
	// end 1
	
		
		System.out.println();
		
		
	// 2: FileNotFoundException
		System.out.println("2. FileNotFound");
		
		String fileName = "numbers.txt";
		Scanner fileScanner;
		
		//Echoes the name of the file	
		System.out.println ("The name of the file you want to open is " + fileName);
         
		try
		{ 
			//Attempts to open fileName
			fileScanner = new Scanner (new File (fileName));
		}  
		catch (FileNotFoundException fnfe)
		{
			System.out.println ("Error: File Not Found Exception." + 
				" The file " + fileName + " was not found.");
		}
	// end 2
		
		
		System.out.println();
		
		
	// 3: NegativeArraySizeException
		System.out.println("3. NegativeArraySize");
		
		int num = -5;
		
		try
		{
			//Attempts to initialize an array of negative size
			int[] array = new int[num];		
		}
		catch(NegativeArraySizeException nae)
		{
			System.out.println("Error: Negative Array Size Exception.  " + 
				"You may not create an array of size: " + num);
		}
	// end 3
				
		
		System.out.println();
		
		
	// 4: NumberFormatException
		System.out.println("4. NumberFormat");
		
		String numToStr = "boobooboo";
		int formatInt;
		
		//Echoes the string being formatted
		System.out.println("String '" + numToStr + "' is to be formatted as an integer.");
		
		try
		{
			//Attempts to format a string as an integer
			formatInt = Integer.parseInt(numToStr);
		}
		catch(NumberFormatException nfe)
		{
			System.out.println ("Error: Number Format Exception." + 
				" The string '" + numToStr + "' is not a legal string to be formatted.");
		}
	// end 4
		
		
		System.out.println();
		
		
	// 5: ArithmeticException
		System.out.println("5. Arithmetic");
		
		int dividend;
		
		//Outputs the attempted operation
		System.out.println("4 / 0 = ?");
		
		try
		{
			//Attempts to divide a number by 0
			dividend = 4 / 0;
			
			System.out.println(dividend);
		}
		catch(ArithmeticException ae)
		{
			System.out.println ("Error: Arithmetic Exception. You may not divide by zero.");
		}
	// end 5		
	
	
		System.out.println();
	
	
		//States that the program has ended normally
		System.out.println("*The program has ended normally.");
	
	} //end main
	
} //end Exceptions