0

May I ask a question regarding to my title. As I were a new comer in java, I a bit confusing with exception handling in my program. What I need to do is, the exceptions need to loop when the user enter the wrong input. For the below coding, its only execute the ArrayOutOfBounds exception. Why the FormatNumberException does not executed when I enter the string/symbol/decimal values. Its only shown in the output section but not in the showMessageDialog output. I try to use multiple exception handling Catch Multiple Java Exception and still the same. Can you help me with this problem. Any answers will be appreciated. Sorry for my bad English.

public class TestRandomArray {

    public static void main(String[] args) {
        Random ran = new Random(); // create instant object from class Random.
        int array[] =new int[100]; //set an array variable with array size of 100
        int maxNumber = 150; //set the limit of the random number to generate

        for (int i = 0; i < array.length; i++) { 
            array[i] = ran.nextInt(maxNumber) + 1; //+1 means that the array number should not contain with 0 value. 1-150.
            //System.out.println(array[i]);                
        }
        //for(int i : array){
            //System.out.println(i + ",");  //this for method is other technique to print the list of array.
        //}        
            do {   
                    String input = JOptionPane.showInputDialog(null, "Enter index"); //Get input from user
                    int choosenIndex = Integer.parseInt(input); //change the string to int type.
                try {  
                    if(choosenIndex >= 0 || choosenIndex <= array.length)  
                        JOptionPane.showMessageDialog(null, "The value of choosen index in the array is : "
                        + array[choosenIndex]); //display the choosen index value.
                        System.exit(0);
                }catch (ArrayIndexOutOfBoundsException e){
                    if (choosenIndex < 0 || choosenIndex > array.length) {
                        JOptionPane.showMessageDialog(null,
                        "Index is out of bound. Enter from 0-99"); //display error for indexoutbound.
                    }
                }catch(NumberFormatException e){
                        JOptionPane.showMessageDialog(null, "Only integer allowed.");//display other error occured.
                }
            } while (true);
    }
}
Community
  • 1
  • 1

3 Answers3

3

Integer.parseInt(input);

This statement is outside of your try block. It has to be inside in order for your program to jump into the catch(NumberFormatException e) block.

Philipp Murry
  • 1,660
  • 9
  • 13
  • if variable 'choosenIndex' wrap's inside try block then it will raise compile-time exception because variable too used into catch block also. – Vishal Gajera Oct 28 '15 at 09:38
  • @Mr.VishalJGajera Then you should declare it outside the try/catch block. – Matt Oct 28 '15 at 09:55
1

public class TestRandomArray {

public static void main(String[] args) {
    Random ran = new Random(); // create instant object from class Random.
    int array[] =new int[100]; //set an array variable with array size of 100
    int maxNumber = 150; //set the limit of the random number to generate

    for (int i = 0; i < array.length; i++) { 
        array[i] = ran.nextInt(maxNumber) + 1; //+1 means that the array number should not contain with 0 value. 1-150.

    }

        do {   
             int choosenIndex = 0;
                String input = JOptionPane.showInputDialog(null, "Enter index"); //Get input from user

            try {  

                  Integer.parseInt(input); //change the string to int type.
                if(choosenIndex >= 0 || choosenIndex <= array.length)  
                    JOptionPane.showMessageDialog(null, "The value of choosen index in the array is : "
                    + array[choosenIndex]); //display the choosen index value.
                    System.exit(0);
            }catch (ArrayIndexOutOfBoundsException e){
                if (choosenIndex < 0 || choosenIndex > array.length) {
                    JOptionPane.showMessageDialog(null,
                    "Index is out of bound. Enter from 0-99"); //display error for indexoutbound.
                }
            }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null, "Only integer allowed.");//display other error occured.
            }
        } while (true);
}

}

nullPointer
  • 116
  • 5
0

Please do following changes only, it works,

  int choosenIndex = 0;
            do {   
                    String input = JOptionPane.showInputDialog(null, "Enter index"); //Get input from user
                try {  
                     choosenIndex = Integer.parseInt(input); //change the string to int type.
                    if(choosenIndex >= 0 || choosenIndex <= array.length)  
                        JOptionPane.showMessageDialog(null, "The value of choosen index in the array is : "
                        + array[choosenIndex]); //display the choosen index value.
                        System.exit(0);
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null, "Only integer allowed.");//display other error occured.
                }catch (ArrayIndexOutOfBoundsException e){
                    if (choosenIndex < 0 || choosenIndex > array.length) {
                        JOptionPane.showMessageDialog(null,
                        "Index is out of bound. Enter from 0-99"); //display error for indexoutbound.
                    }
                }
            } while (true);
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55