0

I have this assignment for a class- a postfix evaluator using stacks and queues. The elements are in a queue entering the evaluation method, and a stack should be used to store elements; however, for some reason I cannot push the final value onto the stack. The method is below:

private String processPostfix(MyQueue postfix)
{
    postfix.viewQueue();
    MyQueue temp=postfix;
    MyStack nums=new MyStack();
    String t="";
    String final_value="";
    int a2;
    int a1 = 0;

    while(!temp.isEmpty()){
        t=(String)postfix.dequeue();
        if(t.equals("+") || t.equals("*") || t.equals("-")){
            a2=Integer.parseInt(nums.pop().data);
            a1=Integer.parseInt(nums.pop().data);
            if(t.equals("+")){
                t=Integer.toString(a1+a2);
            }
            else if(t.equals("*")){
                t=Integer.toString(a1*a2);
            }
            else if(t.equals("-")){
                t=Integer.toString(a1-a2);
            }
        }
        nums.push(new StackNode(t));
    }
    final_value=nums.pop().data;
    return final_value;
}

I can get the right answer to appear if I use

final_value=t;

But the problem is is that continuing to use the calculator, since the final value is not on the stack, any further input cannot be processed against it. I therefore get a NullPointerException error. The test temporary solution for this has been to ignore the empty stack error using this code:

if(!nums.isEmpty()){
    a1=Integer.parseInt(nums.pop().data);
}

I would really appreciate any explanation of this error.

2rtmar
  • 123
  • 1
  • 1
  • 6
  • You *have* pushed the final value onto the stack. Every time you execute `nums.push(new StackNode(t))` you are pushing a value, and the final time you execute it you are pushing the final value. NB You don't need `temp`: you already have it in `postfix`. – user207421 Feb 14 '18 at 04:42
  • I think it should be pushed onto the stack, but final_value=nums.pop().data; gives a NullPointerException error, as though there is nothing there. That's what I don't understand. @EJP – 2rtmar Feb 14 '18 at 04:45
  • Gives it with what input? – user207421 Feb 14 '18 at 04:53
  • For example, 5+3*12-206, the calculator will give us -165; if I click on +5, then the = button (this project is way more complex than I would've liked), then it does not load value and returns the exception. The postfix equation is correct, it gets translated into 5 3 12 * + 206 - @EJP – 2rtmar Feb 14 '18 at 04:55
  • 1
    Given that it seems you implemented the stack and queue classes yourselves, have you considered there's a bug in them? have you tried replacing them with the standard java library classes? did you step through your program with the debugger? does it give the same results for simpler input? Also what does `viewQueue()` do? – Mor A. Feb 14 '18 at 05:10
  • Adding to my above comment: I took your code snippet, pasted it into a file with a simple main class with a simple test, implemented MyStack and MyQueue as simple wrappers around the java library's `Deque`, and ran your code, and it works(implemented `viewQueue()` as a no-op). Based on the error message I guess it either has to do with what `viewQueue()` does, or your `MyStack` implementation. (There is one thing I changed in your code, that is the `new StackNode(t)`, I replaced it with `t` since the user of the `Stack` shouldn't care there are `StackNode`s – Mor A. Feb 14 '18 at 05:48
  • @JimGarrison Give it a rest. He knows why the NPE occurs. The outer question is what to do about it, and it's been answered. – user207421 Feb 14 '18 at 07:44

1 Answers1

0

You need to declare the stack at class level, and peek it, not pop it, to get the final result. Your calculator also needs a C(lear) button to empty the stack.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you! This was part of it. I made these changes and still produced a NullPointerException; as it turns out, my Professor's implementation of stack does not work as designed (though it should), so I just rewrote everything so that the push method would actually work. – 2rtmar Feb 14 '18 at 14:12