编译但是EmptyStackException

时间:2015-07-02 22:17:43

标签: java stack

我正在写这个作业,我想我拥有的一切都是我需要的。当我运行它时,它编译然后一旦我输入一些中缀问题来测试它我得到一个EmptyStackException。

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

//InfixtoPostfix class inherits Stack methods so you can use push(), pop() and peek()
public class InfixtoPostfix extends Stack {
    Stack st = new Stack();

    public String InToPost(String infixString) {
        // declare and initialize postfixString
        String postfixString = "";
        // now parse the infixString character by character
        for (int index = 0; index < infixString.length(); ++index) {
            // declares new stack
           Stack st = new Stack();
            // declares topStack as type char
            char topStack;
            // splits up the infixString to single chars
            char parseString = infixString.charAt(index);
            // if the next character is a left brace add it to the stack
            if (parseString == '(') {
                st.push(parseString);
                /*
                 * if the next character is a right brace set //the top Stack st
                 * as the variable topStack while the stack is not empty and top
                 * of the stack is NOT left brace add topStack to end of
                 * postfixString and pop the stack, then set topStack as the top
                 * st.
                 */

            } else if (parseString == ')') {
                topStack = (char) st.peek();
                while (!st.isEmpty() && (char) st.peek() != '(') {
                    postfixString = postfixString + topStack;
                    st.pop();
                    topStack = (char) st.peek();
                }
                /*
                 * runs if the next character is a '+' or '-' if stack is empty
                 * push the character onto the stack otherwise set topStack as
                 * the top of st and while st is not empty and topStack is not
                 * left or right brace pop the stack and add topstack onto the
                 * postfixString, then push the character onto the stack
                 */
            } else if (parseString == '+' || parseString == '-') {
                if (st.isEmpty()) {
                    st.push(parseString);
                }

                else {
                    topStack = (char) st.peek();
                    while (!st.isEmpty() || topStack != '(' || topStack != ')') {
                        st.pop();
                        postfixString = postfixString + topStack;
                    }
                    st.push(parseString);
                }
                /*
                 * if the next character is '*' or '/' and if the stack is empty
                 * push it onto the stack, else set topStack as the top of st
                 * and while the stack is not empty and topStack does not equal
                 * '+' or '-' pop the stack and add topStack onto the
                 * postfixString, then push the character onto the stack
                 */
            } else if (parseString == '*' || parseString == '/') {
                if (st.isEmpty()) {
                    st.push(parseString);
                } else {
                    topStack = (char) st.peek();
                    while (!st.isEmpty() && topStack != '+' && topStack != '-') {
                        st.pop();
                        postfixString = postfixString + topStack;
                    }
                    st.push(parseString);
                }
                /*
                 * if the character is not an operator then addit onto the
                 * postfixString
                 */
            } else {
                postfixString = postfixString + parseString;
            }
            /*
             * while the stack is not empty set topStack as the top of st if
             * topStack is not a left brace pop the stack and add topStack onto
             * the postfixString
             */

            while (st.isEmpty()) {
                topStack = (char) st.peek();
                if (topStack != '(') {
                    st.pop();
                    postfixString = postfixString + topStack;
                }
            }
        }
        // returns the postfixString
        return postfixString;
    }

    /*
     * complete this code if parseString is left brace, then push it to stack
     * else if the character is right brace: a. Declare a variable of type char
     * called topStack to store the top of stack you might need to cast the top
     * of stack to char using (char) b. Write a while loop. while topStack is
     * not left brace AND stack is not empty 1- add topStack to postfixString 2-
     * pop the stack 3- set topStack to store the new top of stack exit the
     * while then pop the stack else if parseString is '+' or '-' if the stack
     * is empty, push parseString to stack else declare topStack, store the top
     * stack in topStack variable while the stack is not empty OR topStack is
     * not left brace OR topStack is not right brace pop the stack add topStack
     * to postfixString exit the while then push parseString to stack else if
     * parseString is '*' or '/' if the stack is empty, push parseString to
     * stack else declare topStack then store the top stack in topStack variable
     * while the stack is not empty AND topStack is not '+' AND topStack is not
     * '-' pop the stack add topStack to postfixString exit the while then push
     * parseString to stack else add parseString to postfixString
     * 
     * while stack is empty declare topStack variable to store top stack if
     * topStack is not left brace then pop the stack and add topStack to
     * postfixString exit the while loopreturn postfixString. This is the return
     * of InToPost method
     */

    public static void main(String[] args) {
        InfixtoPostfix mystack = new InfixtoPostfix();
        System.out.println("Type in an infix string followed by key");
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        System.out.println("The Expression you have typed in infix form :\n"
            + str);
        System.out.println("The Equivalent Postfix Expression is :\n"
            + mystack.InToPost(str));
    }
}

1 个答案:

答案 0 :(得分:1)

在以下代码中:

while (st.isEmpty()) {
  topStack = (char) st.peek();
  if (topStack != '(') {
    st.pop();
    postfixString = postfixString + topStack;
  }
}

您在堆栈isEmpty()时进行迭代。如果您在空堆栈上执行peek(),则会抛出EmptyStackException

将条件替换为:

while (!st.isEmpty())
相关问题