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

有帮助吗?

解决方案

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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top