0

If a NumberFormatException is thrown when I parse a double from a string (given by the user), how can I retry?

String input = JOptionPane.showInputDialog(null, message + count);
double inputInteger = Double.parseDouble(input);
laalto
  • 150,114
  • 66
  • 286
  • 303
  • See http://stackoverflow.com/questions/8391979/does-java-have-a-int-tryparse-that-doesnt-throw-an-exception-for-bad-data – Tim S. Jun 16 '13 at 12:28

4 Answers4

0
inputInteger = null;

while(inputInteger == null)
{
    input = JOptionPane.showInputDialog(null, message + count);
    try
    {
        if (isValidGrade(input, maxPoints))
            inputInteger = Double.parseDouble(input);
    }
    catch(NumberFormatException e)
    {
        // Show your error here
        inputInteger = null;
    }
}
Devolus
  • 21,661
  • 13
  • 66
  • 113
0

You need to wrap your code in try catch and handle the exception to do whatever you want. Do sth like this:

       try {
                inputInteger = Double.parseDouble(input);
       }catch(NumberFormatException nfe) {
           // Go to take input again
       }
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • *// Go to take input again* :Please explain how will he do that from inside the catch ? – AllTooSir Jun 16 '13 at 12:31
  • 1
    Idea is not to spoon feed people here. He needs to understand how to catch exception and work accordingly. We are not here to code the entire thing here. – Juned Ahsan Jun 16 '13 at 12:33
  • *Idea is not to spoon feed people here. He needs to understand how to catch exception and work accordingly.* He knows he is getting an exception , he wanted to know how to get the i/p again ! – AllTooSir Jun 16 '13 at 12:33
  • Well then just leave a comment instead of an answer which is not an answer. – Micha Wiedenmann Jun 16 '13 at 12:33
0

You can do this by calling the method recursively in the catch block. For an example:

public void yourMethod() {
     try {
          input = JOptionPane.showInputDialog(null, message + count);
          if (isValidGrade(input, maxPoints)){
          inputInteger = Double.parseDouble(input);
    } catch (NumberFormatException e) {
          this.yourMethod();
    }
}

This is not a working code but use the concept in your code. Using a while loop is also another option. But I prefer this method over a while loop because this reduces the memory overhead.

Sithum
  • 126
  • 7
  • 2
    "reduces the memory overhead"? Actually, this uses *more* memory than a loop would. – Ryan Stewart Jun 16 '13 at 12:55
  • This can go into infinite loop!! – Lokesh Jun 16 '13 at 13:54
  • Yes, my bad. Actually it doesn't reduces the memory overhead. It seems I had a totally wrong understanding of the this concept. I should read more about it. Thanks for the comment Stewart. – Sithum Jun 16 '13 at 14:05
0

You can use a do-while loop to repeat the things in this case and make boolean variable false when all conditions are met.

Here is an example:

    boolean isFailure=true;
     do{
        try{
            input = JOptionPane.showInputDialog(null, message + count);
            // do whatever you want....here...
            isFailure=false;

       }catch(NumerFormatException e){
            //log the exception and report the error
            JOptionPane.showMessageDialog(null,"Invalid Input! Try again!", "Error", 
 JOptionPane.ERROR_MESSAGE); 

         }

         }while(isFailure);
pinkpanther
  • 4,770
  • 2
  • 38
  • 62