EmptyStackException

后端 未结 3 705
不思量自难忘°
不思量自难忘° 2021-01-13 22:53

This EmptyStackException continues to pop up. Obliviously there is nothing in my stack, but the first element that the User inputs. However, I am not sure where the code i

3条回答
  •  执笔经年
    2021-01-13 23:31

    A stack is LIFO, or "Last in, First out". So when you have an input sequence of 4 5 * 6 - and do this:

    rpnStack.push(st.nextToken());
    

    The first thing you pop will be "-", the second thing will be "6", and the third thing will be "*". Is this what you expect?

    Also, instead of:

    String temp1 = stack.peek();
    stack.pop();
    

    You can do:

    String temp1 = stack.pop();
    

提交回复
热议问题