import java.util.*;
import java.io.*;

/*******************************************************************
 * This program tries to use five try-catch blocks to handle 
 * five different Exceptions: FileNotFoundException,
 *										InputMismatchException,
 *										NumberFormatException, 
 *										ArithmeticException,
 *										StringIndexOutOfBoundsException.
 *
 * @author Lu Tian
 * @version 3
 *******************************************************************/
 // Date: January 17, 2007
 // Section 1
 // Lab 3
 public class Exceptions_Lab_tianlx_ExceptionsHandling
 {
 	/**
	 * The main() method show that how to create five exception handlers
	 * to elegantly handle the exceptions.
	 *
	 * @param args		Command line (unused in this program)
	 * @exception 		FileNotFoundException	When there cannot find such file
	 *	@exception		InputMismatchException	When any input is not match
	 *	@exception		NumberFormatException	When user's input is not a number
	 *	@exception		ArithmeticException		When divided by zero
 	 *	@exception		StringIndexOutOfBoundsException	When String array out of bounds
	 **/
	public static void main(String[] args)
	{
		boolean	done;					
		int 		integers, num;		// The integers from user's file; A number that parse from user input 
		int		ratio;				// Use two user's inputs to count ratio
		int		number1, number2; // User enter two integers
		Scanner	input, fileScan;
		String	fileName;
		String	anyNumber, words;
		
		done  = false;
		input = new Scanner(System.in);
		
		// The First try-catch block
		System.out.println("--- The First try-catch block --- \n");
		
		try
		{
			System.out.println("Please enter file name that hold integers: ");
			fileName = input.nextLine();
			
			// Echo the input file name
			System.out.println("The file you want to open is: " + fileName + "\n");
			
			
			fileScan = new Scanner(new File(fileName));
			
			// The Second try-catch block is inside of the first try-catch block
			try
			{
				System.out.println("--- The Second try-catch block (inside of the first block) --- \n");
				while(!done)
				{
					integers = fileScan.nextInt();
				
					// Echo the each integer that comes from user's file
					System.out.println("The integer in your file is: " + integers);
				}
			}
			catch(InputMismatchException ime)
			{
				System.out.println("The numbers in your file are NOT all integers.\n");
			}
			
		}
		catch(FileNotFoundException fnfe)
		{
			System.out.println("The file you wanted to open is NOT found. "
								+ "Please check your file name again.\n");
		}
		
		
		//The Third try-catch block
		System.out.println("--- The Third try-catch block --- \n");
		System.out.println("Please enter any number (it must be an integer): ");  
		anyNumber = input.next();
					
		// Echo the input that comes from user
		System.out.println("You entered: " + anyNumber + "\n");
		
		try
		{	
			num = Integer.parseInt(anyNumber);
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("The " + anyNumber + " you entered is not an integer number.\n");
		}
		
		
		// The Forth try-catch block
		System.out.println("---- The Forth try-catch block, which count ratio ---\n");
		
		try
		{
			System.out.println("Please enter the first integer, which is the Numerator: ");
			number1 = input.nextInt();
			
			//Echo the first integer that user entered
			System.out.println("The first integer that you entered is: " + number1 + "\n");
			
			
			System.out.println("Please enter the second integer, which is the Denominator: ");
			number2 = input.nextInt();
			
			//Echo the second integer that user entered
			System.out.println("The second integer that you entered is: " + number2 + "\n");
			
			ratio = number1 / number2;
			System.out.println("The ratio of two integers is: " + ratio + "\n");
		}
		catch(ArithmeticException ae)
		{
			System.out.println( "Wrong input integer: the second integer, which is the Denominator, cannot be 0.\n" );
		}
		
		
		//The Fifth try-catch block
		System.out.println("--- The Fifth try-catch block ---\n");
		words = "abcd";
		
		try
		{
			System.out.println(words.charAt(4));
		}
		catch(StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("The String index is out of rang.\n");
		}
		
		
		//Final comment of this program
		System.out.println("The program is end successfully.");
		
	} // end of main()
	 
 } // end of class