Java 编译但清空堆栈异常

Java 编译但清空堆栈异常,java,stack,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 InfixtoPostf

我写这篇文章是为了完成一项任务,我想我已经拥有了我所需要的一切。当我运行它时,它会编译,但是一旦我输入一些中缀问题来测试它,就会得到一个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));
    }
}
import java.util.Scanner;
导入java.util.Stack;
//InfixtoPostfix类继承堆栈方法,因此可以使用push()、pop()和peek()
公共类InfixtoPostfix扩展堆栈{
Stack st=新堆栈();
公共字符串InToPost(字符串infixString){
//声明并初始化postfixString
字符串postfix字符串=”;
//现在逐个字符解析infixString
对于(int index=0;indexwhile (st.isEmpty()) {
  topStack = (char) st.peek();
  if (topStack != '(') {
    st.pop();
    postfixString = postfixString + topStack;
  }
}
while (!st.isEmpty())