2014-04-16 37 views
-2

所以我试图用教授给我的驱动程序文件做一个BalancedSymbols类。我在某些情况下使用它,但在其他情况下,我得到EmptyStackException错误。EmptyStackException错误

这里是提示:

The BalancedSymbols class is a utility class that will be used to check if parentheses (), brackets [], and braces {} are matched in a given string. The BalancedSymbols class object will never be instantiated. It has the following method: 

public static String balancedSymbols(String lineToCheck) 

The balancedSymbols method's argument is a string that can contain parenthesis, brackets, and braces. Other characters or numbers can appear before/after/between them. 

If all symbols ({([) matched, then the method should return the string: 

"Everything is matched!" 

If there is a closing parenthesis, bracket, or brace that does not have its corresponding opening parenthesis, bracket, or brace, 
then for the first such character, return a string with its position such as: 

") at the position 15 does not match." 

"] at the position 12 does not match." 

OR 

"} at the position 28 does not match." 

The first character of the string is at position 0. 

If there are no matching closing parenthesis, bracket, and/or brace when you reach the end of the string after checking each character, return a string notifying the last opening parenthesis, bracket, or brace that did not have its matching closing one as: 

") is missing." 

"] is missing." 

OR 

"} is missing." 

Requirement: 

You need to implement this method using a Stack from java.util package. 

You can use the string "A" as a token for parentheses, "B" as a token for brackets, "C" as a token for braces. 

When an opening parenthesis is read, push the token "A", and when a closing parenthesis is read, pop the token "A" if available. 
A similar operation can be done for others. Only if the stack is empty when you finish reading all characters of the string, you can confirm that everything was matched. Otherwise, the method needs to return an appropriate message stated above 

这是给我的驱动程序文件:

import java.io.*; 

public class Assignment11 
{ 
    public static void main (String[] args) throws IOException 
    { 
    char input1; 
    String inputInfo; 
    String line = new String(); 

    printMenu(); 

    InputStreamReader isr = new InputStreamReader(System.in); 
    BufferedReader stdin = new BufferedReader(isr); 

    do // will ask for user input 
    { 
    System.out.println("What action would you like to perform?"); 
    line = stdin.readLine(); 
    input1 = line.charAt(0); 
    input1 = Character.toUpperCase(input1); 

    if (line.length() == 1) 
     { 
     // matches one of the case statements 
     switch (input1) 
     { 
     case 'E': //Enter String 
      System.out.print("Please enter a string.\n"); 
      inputInfo = stdin.readLine().trim(); 
      System.out.println(BalancedSymbols.balancedSymbols(inputInfo)); 
      break; 
     case 'Q': //Quit 
      break; 
     case '?': //Display Menu 
      printMenu(); 
      break; 
     default: 
      System.out.print("Unknown action\n"); 
      break; 
     } 
     } 
    else 
     { 
     System.out.print("Unknown action\n"); 
     } 
    } while (input1 != 'Q' || line.length() != 1); 
    } 


    /** The method printMenu displays the menu to a user**/ 
    public static void printMenu() 
    { 
    System.out.print("Choice\t\tAction\n" + 
        "------\t\t------\n" + 
        "E\t\tEnter String\n" + 
        "Q\t\tQuit\n" + 
        "?\t\tDisplay Help\n\n"); 
    } 
} 

这里是我的编码:

import java.util.Stack; 
public class BalancedSymbols 
{ 
    private static final String OPEN = "([{"; 
    private static final String CLOSE = ")]}"; 

    public static String balancedSymbols(String lineToCheck) 
    { 
    Stack<String> stack = new Stack<String>(); 
    boolean balanced = true; 

    int index = 0; 
    String curr = ""; 
    String top = ""; 
    char missedChar = ' '; 
    while(balanced && index < lineToCheck.length()) 
    { 
     curr = lineToCheck.substring(index, index + 1); 

     if(OPEN.indexOf(curr) > -1) 
     { 
      stack.push(curr); 

      missedChar = CLOSE.charAt(OPEN.indexOf(curr)); 
     } 
     else if(CLOSE.indexOf(curr) > -1) 
     { 
      top = stack.pop(); 

      if(OPEN.indexOf(top) != CLOSE.indexOf(curr)) 
      {      
       balanced = false; 
       break; 
      } 

     }   

     index++; 
    } 

    if(index > lineToCheck.length()) 
    { 
     missedChar = CLOSE.charAt(OPEN.indexOf(stack.peek())); 
    } 

    if(balanced && stack.isEmpty()) 
    { 
     return "Everything is matched!"; 
    } 
    else if(index < lineToCheck.length()) 
    { 
     return missedChar + " at the position " + index + " does not match.";  
    } 
    else 
    { 
     return missedChar + " is missing."; 
    } 
    } 

} 

我能但是当我输入类似“[[[[] [] [] [] []]]]] [[]] [] [] [] []]]]”的测试用例时,我得到一个EmptyStackException错误,以及其他一些输入。

除此之外,其他输入给我错误的括号类型,如缺少一个'}'而不是'}'。

我对编写堆栈很新,所以我很感谢任何帮助我的代码。这也是我的第一个问题,所以我很抱歉,如果它很长或很难阅读。

+1

是的,它很长很难阅读。你有没有在调试器中加入代码? – OldProgrammer

回答

0

这里是如何开始的提示:

  1. 不需要数组列表。你创建一个Stack对象。堆栈myStack =新堆栈();

  2. 你需要使用:PUSH,POP,和PEEK方法

  3. 你需要一个while循环,从字符串中读出的第一个字符(lineToCheck)

,如果它是一个如果当前字符是右括号,检查是否最后一个是左括号(使用peek方法)

如果它是弹出的,否则设置您的布尔值以退出循环并打印在哪里该字符不匹配。

  1. 现在,在结束时,如果你达到字符串的最后一个字符,并检查是否堆栈是空的相同的托架

  2. 同捆带

  3. 。如果不是,那么最后一个缺失

我希望这有助于!