This is in Java. In this class I am evaluating a postfix string by iterating through a for loop. I want to set x and y equal to the operand values by popping the stack. However I don't know how to pop the stack values of a char and set them equal to an integer.
import java.io.*;
public class EvalPostfix {
private ObjectStack stack = new ObjectStack();
private PrintWriter p;
private int post;
private int x;
private int y;
private char z;
public EvalPostfix(PrintWriter pw) {
p = pw;
}
public int evalPostfix(String postfix) {
/**
* creates for loop to iterate through the characters of postfix
* if char is an operand push it to the stack
*/
for(int i = 0; i < postfix.length(); i++) {
z = postfix.charAt(i);
if(z >= 48 && z<= 57){
stack.push(z);
}
else {
while(!(stack.isEmpty())){
x += (Integer) stack.pop();
y += (Integer) stack.pop();
}
}
if(z == '+') {
stack.push(y+x);
}
if(z == '-') {
stack.push(y-x);
}
if(z == '*') {
stack.push(y*x);
}
if(z == '/') {
stack.push(y/x);
}
if(z == '^') {
stack.push(y^x);
}
post += (integer) stack.pop();
}
return post;
}
}