Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions