Question: java calculator program I need help adding these excemptions in my postfix method -4 2 + - 1 * 1 + = Exception caught: missing

java calculator program

I need help adding these excemptions in my postfix method

-4 2 + - 1 * 1 + = Exception caught: missing operand -3 5 = Exception caught: missing operator

public static double evalPostfix(String s) throws IllegalArgumentException { Stack stack=new Stack<>(); char[] tokens = s.toCharArray(); // Scan all characters one by one for(int i=0;i 1 || isNum(sbuf.toString())) {

stack.push(Double.parseDouble(sbuf.toString())); } // If the scanned character is an operator, pop two // elements from stack apply the operator else { double val1 = stack.pop(); double val2 = stack.pop(); switch(tokens[i-1]) { case '+': stack.push(val2+val1); break; case '-': stack.push(val2- val1); break; case '/': stack.push(val2/val1); break; case '*': stack.push(val2*val1); break; default : System.out.println("Invalid token"); break; } } } return stack.pop(); } public static boolean isNum(String strNum) { boolean ret = true; try {

Double.parseDouble(strNum);

}catch (NumberFormatException e) { ret = false; } return ret; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!