user7154867
user7154867

Reputation:

Java - Try/Catch NumberFormatException uses a former value?

First post so my apologies if this was done incorrectly (and am also relatively new to programming, so any extraneous tips are also appreciated).

So I have written up a basic calculator program in Java. It works well currently, but I am having a particular issue with NumberFormatException. Here's the code:

private static double spaceTestAndConvert(String numInput){

    Scanner input= new Scanner(System.in);

    if (numInput.equalsIgnoreCase("quit")){
        System.exit(1);
    }
    else if(numInput.equalsIgnoreCase("C/E")){
        Restart();
    }

    try{
        return Double.parseDouble(numInput.trim());
    }
    catch(NumberFormatException nfe){
        numInput = "";
        System.out.println("Please enter only one number without any spaces or letters: ");
        numInput = input.nextLine();
        spaceTestAndConvert(numInput.trim());
        return Double.parseDouble(numInput.trim());
    }
}

The issue is that after trying to force an error by entering in several inputs which would cause NumberFormatException and then entering in a valid input, the program will crash from a NumberFormatException citing the previous invalid input.

I.E. -

"1 2 3"
loops back
"1 2   q 3"
loops back
"12q3   3   sqw 1"
loops back
"12"
crash - Exception in thread "main" java.lang.NumberFormatException: For input string: "12q3   3   sqw 1"

It only occurs after several occurrences of the exception. I'm curious why it is doing this. Any advice on how to fix this or explanation of what is happening? If you need any other part of the code, please let me know! Thanks!

Upvotes: 0

Views: 303

Answers (2)

jace
jace

Reputation: 1674

removing the return in catch will solve your problem. because if you have return on it, you are going to return an invalid Number format since you are in a catch. What you want to do is to return a value when it is now valid, you are now actually doing it inside the try. Don't force your program to return the value with error (since it is in a catch) because it will really give you an error.

returning to previous method after you had the right value (because of recursion) will still have the stack of error value aside from success value you gained from the end part because they are different variables.

private static double spaceTestAndConvert(String numInput){

Scanner input= new Scanner(System.in);

if (numInput.equalsIgnoreCase("quit")){
    System.exit(1);
}
else if(numInput.equalsIgnoreCase("C/E")){
    Restart();
}

try{
    return Double.parseDouble(numInput.trim());
}
catch(NumberFormatException nfe){
    numInput = "";
    System.out.println("Please enter only one number without any spaces or letters: ");
    numInput = input.nextLine();
    spaceTestAndConvert(numInput.trim());
}
}

Upvotes: 0

Drew Wills
Drew Wills

Reputation: 8446

I don't follow everything that you're saying, but these 2 lines (from within your catch block) look problematic...

spaceTestAndConvert(numInput.trim());
return Double.parseDouble(numInput.trim());

You are calling the spaceTestAndConvert function recursively, but throwing away the value. I don't understand why you would call it and not be interested in the value.

The second line is also a mess. You so carefully surround the first call to Double.parseDouble() with try/catch, but then you call it again within your catch block. If the second Double.parseDouble() generates a NumberFormatException, it will not be caught.

Upvotes: 2

Related Questions