2015-07-02 28 views
-4

我寫這個作業,我想我有我需要的一切。當我運行它時,它會編譯,但是一旦我輸入一些中綴問題來測試它,我會得到一個EmptyStackException。編譯但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)); 
    } 
} 
+0

不要使用esception來控制你的邏輯流。 –

回答

1

在下面的代碼:

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

你迭代而堆棧isEmpty()。如果您在空堆棧上執行peek(),則會引發EmptyStackException

與更換條件:

while (!st.isEmpty()) 
+0

哦,我明白了,謝謝! – Kyle

+0

@凱爾不客氣。考慮接受答案,如果它有幫助。 – mziccard