the program works the way i want it to except for the inputmismatchexception.

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;


public class Exception {

    public static void main(String[] args) throws IOException {

        int num = 100;
        int tempNum;
        int index;
        String[] strArray = new String[num];
        int[] numArray = new int [num];

        File tempo = new File ("exceptionData.txt");
        Scanner file = new Scanner(tempo);

        for( int i = 0; i < numArray.length; i++)
        {
            strArray[i] = file.next();
            numArray[i] = Integer.parseInt(strArray[i]);
        }


        Scanner input = new Scanner (System.in);

        while(input.hasNextInt())
        {
            index = input.nextInt();

            try
            {


                    if(index == -1)
                    {
                        System.exit(0);
                    }



                System.out.print(index + ": " + numArray[index] + ", ");

            }
            catch(ArrayIndexOutOfBoundsException ex) 
            {
                System.out.println("\nException: " + index + " is Out of bounds");
            }
            catch (InputMismatchException ex)
            {
                System.out.println("\nException: Enter an integer (-1 to quit)\n");        
            }

        }



}



}

OUTPUT should look like this

enter numbers: 0 2 3 9 -100 -10 A
0: 412, 2: 201, 3: 397, 9: 750,
Exception: -100 is Out of Bounds
Exception: -10 is Out of Bounds //array indexoutof bounds
Exception: Enter an integer (-1 to quit) //inputmismatch

Your catches will catch exceptions thrown inside the corresponding try block (lines 35-47). The nextInt method (line 32) can throw the IMM exception, but that's outside your try.

i was able to make it work but because i put the try block in the while loop the IMM loops infinitely. Now what I am trying to do is input letters like a b c then output the IMM 3 times since it found 3 mismatch

while(input.hasNext())
        {
            try
            {
            index = input.nextInt();

            try
            {
                    if(index == -1)
                    {
                        System.exit(0);
                    }

                System.out.print(index + ": " + numArray[index] + ", ");


            }


            catch(ArrayIndexOutOfBoundsException ex) 
            {
                System.out.println("\nException: " + index + " is Out of bounds");
            }
            }
            catch (InputMismatchException ex)
            {
                System.out.println("\nException: Enter an integer (-1 to quit)\n");        
            }


        }

as long as you print the "Exception: Enter an integer" ...
or the other catch block, you are still within the while block. if you want this to stop, add a break statement somewhere.

This article has been dead for over six months. Start a new discussion instead.