0

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;
      }
    } 
Valskarx
  • 25
  • 6
  • `ObjectStack` is an individual stack/implementation? It can "pop" only `String`? or ..any type of objects (as the name implies)? ...generally (in java) the conversion int to char is "easy-peasy" (no conversion at all ...directly mapped to ascii-table ... mathematical operations can be applied to char)..https://stackoverflow.com/q/46343616/592355 – xerx593 Mar 06 '20 at 23:38
  • Yeah converting isn't the problem. I just want to pop to stack object to the integer. So I can use those values to do arithmetic. – Valskarx Mar 06 '20 at 23:42
  • so, when it's `[public] String pop()`, then you can `char foo = stack.pop().charAt(0);` (for the char value) or `int bar = stack.pop().charAt(0) - '0';` (for the "integer value of a digit char") – xerx593 Mar 06 '20 at 23:46
  • "The method charAt() is undefined for the type Object" – Valskarx Mar 06 '20 at 23:52
  • ...so it's `[public] Object pop()` ..and since you push only Integers and Chars to the stack you could just "try": `x = (Integer) stack.pop();` as `y = (Integer) stack.pop();` – xerx593 Mar 06 '20 at 23:56
  • I now get a stack underflow. – Valskarx Mar 06 '20 at 23:58
  • underflow!? (WTH:) -> https://stackoverflow.com/q/35071931/592355 – xerx593 Mar 07 '20 at 00:01
  • "stack underflow" is "custom" ..and i would interpret as "try to pop empty stack" – xerx593 Mar 07 '20 at 00:05
  • Here I'll edit the code, for the changes I've made. – Valskarx Mar 07 '20 at 00:05
  • You push each digit to the stack separately? Can the number be more than one digit? – David Conrad Mar 07 '20 at 00:18
  • I have it so in the loop it pushes digits to the stack. And the else statement is the two pops of the operands. I set them to integers and then do the arithmetic. – Valskarx Mar 07 '20 at 00:27

0 Answers0