Question

As a newbie to Java .. How to evaluate postfix expression of multi-digit numbers including negative ones (ie. "12 -12 +")

My snippet gives "java.lang.NullPointerException" !

I used "StringTokenizer" , however it didn't work efficiently

Anyone could appreciately help improving code ?

Thanks in advance

Was it helpful?

Solution

The algorithm is:

  1. read the string from the last element to the first and put each element in a stack of Strings (each element of your stack will be a single char operator or a word convertible to a number).
  2. pop 3 elements convert the first two to a number and depending on the operator calculate the result
  3. push the result on the stack
  4. repeat 2) and 3) till the stack is empty.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top