首页 > 解决方案 > 通用堆栈没有“推送”必要的元素

问题描述

我目前正在调试一些我遇到的问题的代码,该问题应该检查是否存在左括号和右括号的平衡。为此,我使用了我制作的包含所有括号的通用堆栈。这是我的主要代码

/* Following the specification in the README.md file, provide your 
 * SymbolBalance class.
 * test these: { }’s, ( )'s, [ ]'s, " "’s, and /* * /’s are properly balanced
 */ 

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SymbolBalance{
     public static void main(String[] args){
         if(args.length > 0){
             try{
                 Scanner file = new Scanner(new File(args[0]));
                 MyStack<Character> balance = new MyStack<>(); 
                 String string;
                 char character; 
                 char charNext;
                 int line = 0; 
                 boolean beginning = true; 
                 // the easiest way to understand/code this problem is by
                 // reading over each individual string, then each 
                 // individual character of that string
                 while(file.hasNextLine()){
                     line++;
                     string = file.next();
                     for(int i = 0; i < string.length() - 1; i++){
                         character = string.charAt(i);
                         charNext = string.charAt(i + 1);
                         if(character == '[' || character == '{' ||
                            character == '(' || character == '/' && 
                            charNext == '*' || character == '/' && 
                            charNext == '*'){
                            balance.push(character);
                         }
                         else if(character == '*' && charNext == '/'){
                            if(balance.isEmpty()){
                            System.out.println("<"+i+">: Empty");
                            }
                            else if(balance.pop() != '*'){
                                System.out.println("<"+i+">: <"+character+">, <"+balance.pop()+">");
                            }
                        }
                        else if(character == ']'){
                            if(balance.isEmpty()){
                                System.out.println("<"+i+">: Empty");
                            }
                            else if(balance.pop() != '['){
                                 System.out.println("<"+i+">: <"+character+">, <"+balance.pop()+">");
                            }
                        }
                        else if(character == '}'){
                            if(balance.isEmpty()){
                                 System.out.println("<"+i+">: Empty");
                             }
                             else if(balance.pop() != '{'){
                                 System.out.println("<"+i+">: <"+character+">, <"+balance.pop()+">");
                            }
                        }
                        else if(character == ')'){
                            if(balance.isEmpty()){
                                System.out.println("<"+i+">: Empty");
                            }
                            else if(balance.pop() != '('){
                                 System.out.println("<"+i+">: <"+character+">, <"+balance.pop()+">");
                            }
                        }
                        else if(character == '"'){
                            if(beginning == true){
                               balance.push(character);
                            }
                            else{
                                if(balance.isEmpty()){
                                    System.out.println("<"+i+">: Empty");
                                }
                                else if(balance.pop() != '('){
                                     System.out.println("<"+i+">: <"+character+">, <"+balance.pop()+">");
                                }
                                beginning = true; 
                            }
                        }
                    }
                }
                file.close(); 
             }
             catch(FileNotFoundException e){
             System.out.println("No such file exists or cannot be found");
            }
        }
    }
}

发生的情况是,一旦我的代码运行,就会出现一个错误,提示我的堆栈为空(来自我的通用堆栈类)。这让我认为我的角色没有被推入堆栈(因此出现空堆栈异常)。我查看了我的通用堆栈代码,我相当确信问题不在该文件中。我的 if 语句是否有问题,或者这是我没有正确使用扫描仪的问题,因此文件本身没有被正确读取?另一方面,我没有看到应该读取的文件,我知道它不是空的,所以这也不是问题。

我知道这可能是一个愚蠢的错误,但我现在似乎无法区分它。我会很感激任何意见。

谢谢!

标签: javafilestackjava.util.scanner

解决方案


推荐阅读