I wrote up a postfix calculator for boolean expressions given that it was in postfix form. The first time I went about writing it, I used the expression
s.add(s.pop() [operation] s.pop());
where s
is my stack and the operation
is and
, xor
, or or
.
For some reason, the pop
methods did not seem to peel of the value from the stack, so I instead rewrote this operation using two boolean variables: b1
and b2
. These variables contained the popped values which then are passed through the add
method with the proper operation.
if(str.equals("or")){
boolean b1 = s.pop();
boolean b2 = s.pop();
s.add(b1 || b2);
//out.println(s);
}
Why couldn't I use the first statement mentioned?