This is a test build for a new version of the calculator application in which I am trying to implement dynamic mode switching (fairly standard... subtract your total while in addition mode). I'm basically rewriting the entire Arithmetic.java class so that all of the methods have return types rather than declaring them void.
This current test build implements only addition and subtraction with a temporary main method (for testing purposes). Mathematically it works fine. The problem arises with the Try/Catch layout.
[CODE] public class Arithmetic { public Double Add(double totNum) { boolean calc = true; while(calc == true) { try { CalcInput user = new CalcInput(); String userInput = user.getUserInput(" "); if(userInput.equals("-")) { System.out.println(totNum + "-"); double result = Subtract(totNum); totNum = result; } else {} double addNum = Double.parseDouble(userInput); if(totNum == 0) { System.out.println(addNum + "+"); } else { System.out.println(totNum + "+" + addNum); } totNum = totNum + addNum; } catch(NumberFormatException e) { System.out.println("Invalid input! Check to make sure you typed it correctly and try again!"); System.out.println("-DO NOT use operators. Enter your NUMERICAL input and press ENTER."); System.out.println("-You may use shortcuts to call other calculation modes. (+, -, x, etc."); } catch(NullPointerException e) { calc = false; } } return totNum; } public Double Subtract(double totNum) { boolean calc = true; while(calc == true) { try { CalcInput user = new CalcInput(); String userInput = user.getUserInput(" "); if(userInput.equals("+")) { System.out.println(totNum + "+"); double result = Add(totNum); totNum = result; } else {} double addNum = Double.parseDouble(userInput); if(totNum == 0) { System.out.println(addNum + "-"); totNum = addNum; } else { System.out.println(totNum + "-" + addNum); totNum = totNum - addNum; } } catch(NumberFormatException e) { System.out.println("Invalid input! Check to make sure you typed it correctly and try again!"); System.out.println("-DO NOT use operators. Enter your NUMERICAL input and press ENTER."); System.out.println("-You may use shortcuts to call other calculation modes. (+, -, x, etc."); } catch(NullPointerException e) { calc = false; } } return totNum; } public static void main(String[] args) { Arithmetic a = new Arithmetic(); double result = a.Add(0); System.out.println(result); } } [/CODE]
If you call the subtraction method while in the addition mode and press ENTER with nothing in the input field (which I would assume to be a NullPointerException), it displays the error message I added for the NumberFormatException.
Why is that? Is it because you are operating within a call? In that case, how do I get out of the called method so that the person can hit ENTER and get the result?
If it isn't clear yet what the problem is, I will try to clarify a bit more:
While in the program....
//I enter 5. The system prints the response as usual.
5.0+
//I enter 5 again. Still working fine.
5.0+5.0
//I enter - The call works fine.
10.0-
//I enter 5. Still lookin good.
10.0-5
//I hit ENTER (no input). Here is where it goes to hell.
It displays the error message I implemented in the NumberFormatException catch.
//I hit ENTER again. NOW it gets it.
5.0 <----- It got the right answer!
But it's recognizing a NullPointerException as a NumberFormatException while inside the method call.
Any help would be appreciated.
EDIT: I did just realize there are two loops within the try/catch. So the problem probably comes from the fact that it's trying to end the first loop before the inner loop is done executing. But, how do I fix that and/or why is it displaying the defined catch for NumberFormatException?