import java.io.*;
import java.util.*;

/**********************************************************************
*description : Exception handling
*
* This class demonstrates generation of five types of exceptions
* and the code to handle these exceptions. The demonstarted exceptions are:
* - array index out of bounds
* - division by zero
* - file not found
* - input mismatch
* 
*
* @author Ravi Bhatia ;  date 01/22/2007,  LAB3, Section1
*
***********************************************************************/

public class demoExceptionsHandling
{
public static void main(String[] args){

String fileName;
Scanner keyboard;
Scanner fileScanner;
float[] array;
int index=5;
array=new float[index];
keyboard=new Scanner(System.in);
boolean continueInput=true;

//Asks for a filename containing numbers and reads the data into an Array 
do{                         //do loop is to ask for file name again in case of an exception

System.out.println("\nThis program reads integer numbers fom a file. Enter a file name:"); 
array=new float[index];
fileName=keyboard.nextLine();
System.out.println("You have entered: "+fileName);


//looks for File not found exception
try
{
fileScanner=new Scanner(new File(fileName));
int i=0;

while (fileScanner.hasNextFloat())   //reads the file data into an array
{
array[i]=fileScanner.nextFloat();
System.out.println("array element number "+i+" is:"+array[i]);
i++;
}

continueInput=false; //stops the do-while loop
}


//catches 'array index out of bound exception' and increases the array size
catch(ArrayIndexOutOfBoundsException aioob)
{
System.out.println("The array index has gone out of bounds. The array has been defined with 5 elements only,"+
                    " but the text file has more numbers"); 
System.out.println("Increase the array index to read file again. Enter new index value: ");
index=keyboard.nextInt();
System.out.println("The index is: "+index);
keyboard.nextLine();
}



//catches 'File not found exception' and asks for the file name again
catch (FileNotFoundException fnfe)
{
System.out.println("File not found. Try using 'numbers.txt' file");
}
}
while (continueInput);  




//demonstrates InputMismatcgException by prompting to input and integer number
boolean continueNumber=true;
do
{
try
{
//int element;
int element;
Scanner number;
number=new Scanner(System.in);
System.out.println("\n\nNow, enter an integer: ");
element=number.nextInt();
System.out.println("You have entered the integer: "+element+"."+
                    "To see a demo of 'InputMismatchException, run again and enter a floating number");
continueNumber=false;
}
catch(InputMismatchException ime)
{
System.out.println("Wrong data type! Enter again.");
}
}  //end of do block
while (continueNumber);



//demo of 'DivisionByZeroException'
try
{						 
float testNumber;
Scanner input;
input=new Scanner(System.in);
System.out.println("\n\n\nThis demo will ask for a number and divide the number by integers from 5 to 0"+
                   ". Please enter a number:");
testNumber=input.nextFloat();
System.out.println("You have entered the number: "+testNumber);

for(int x=5;x==0;x--)
{
System.out.println(testNumber+" divided by "+x+" = "+testNumber/x);
}
}

catch(ArithmeticException ae)
{
System.out.println("Division by zero has occured");
}
                   




}//end of main
}//end of class