是什么导致我得到这个EmptyStackException?

时间:2015-12-10 01:02:03

标签: java stack runtime-error postfix-notation infix-notation

我的程序采用后缀表达式并将其更改为中缀表达式。

我在代码中包含了两个错误原因,即如果程序没有足够的运算符,并且输入不是有效数字或运算符。

当我输入不好的输入时会捕获错误,但是,当在扫描仪中输入正确的输入时,它会给我这个错误:

Exception in thread "main" java.util.EmptyStackException
    at java.util.Stack.peek(Stack.java:102)
    at java.util.Stack.pop(Stack.java:84)
    at PostfixToInfix.change(PostfixToInfix.java:67)
    at PostfixToInfix.main(PostfixToInfix.java:27)

我的代码需要更改什么? 代码:

import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;

public class PostfixToInfix
{
    int x = 0;

    public static void main(String[] args) 
    {
        PostfixToInfix exp = new PostfixToInfix();
        Scanner stdin =new Scanner(System.in);


        try { 
        boolean inputNeeded = true;
        int value = 0;

        while(inputNeeded){
            System.out.print("Postfix : ");
            if(stdin.hasNextInt()){
                inputNeeded = false;
            }
            else{
                throw new Error("Not a number or valid operator");
            }
        }
            String pf = stdin.nextLine().replaceAll("\\s+", ""); 
            System.out.println("Infix : "+exp.change(pf));

        } 
        catch (EmptyStackException e) {
            System.out.println("Too few operators to produce a single result.");
        }


    }

    static boolean isOperator(char c)
    {
        if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^')
        {
            return true;
        }
        return false;

    }
    boolean empty() //whether the stack is empty
    {
        return x == 0;

    } // end empty

    public String change(String pf)
    {
        Stack<String> s = new Stack<>();

        for(int i = 0; i < pf.length(); i++)
        {
            char z = pf.charAt(i);
            if(isOperator(z))
            {
                String x = s.pop();
                String y = s.pop();
                s.push("("+y+z+x+")");      
            }
            else
            {
                s.push(""+z);
            }

        }   
        return s.pop();
     } 
}

1 个答案:

答案 0 :(得分:0)

Consider the input 1 1 +.

  • The Scanner reads the 1 and stores it in value.
  • The Scanner reads the remaining string and a modified version of it ("1+") is stored in pf, which is passed as an argument to the change method.
  • charAt returns the first character of pf (a '1'), isOperator returns false, and the else block executes, which pushes "1" to the Stack.
  • charAt returns the second character of pf (a '+'), isOperator returns true, and the if block executes.
  • pop is called once, removing the only element in the stack, "1", and assigning it to x. The stack is now empty, and the second call to pop results in the EmptyStackException.

This is how to debug your code if your IDE does not have a debugger already. Through this, you should find that using nextInt is the issue, since only one number will be present in the remaining string when the if block is expecting two.