java - 我的程序符合要求,但不打印任何内容

标签 java

我正在编写这个程序,检查是否 { [ ( " /*} ] ) " */ 我们需要忽略评论 block 中的内容。当我在终端中运行代码时遇到了一个问题,没有任何内容打印出来。我知道错误在哪里,但不知道为什么错误。在我尝试编写忽略注释的代码后,我开始遇到这个奇怪的问题。我们需要编写自己的堆栈类,称为 MyStack.java,但它非常简单,因此我不会在此处包含。

我认为问题发生在这里:

int start = str.indexOf("/*");
int end = str.indexOf("*/");
if(start != -1){
  str = str.substring(0,start) + str.substring(end+2);

这是完整的代码:希望没有任何逻辑谬误

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 input = new Scanner(new File (args[0]));
                MyStack<Character> ms = new MyStack<>();
                String str;
                char ch;
                boolean quoteStart = true;
                loop: while(input.hasNext()){
                    str = input.next();
                    int start = str.indexOf("/*");
                    int end = str.indexOf("/*");
                    if(start != -1){
                      str = str.substring(0,start) + str.substring(end+2);
                        for(int i=0;i<str.length();i++){
                        ch = str.charAt(i);      
                            if(ch == '{'||ch =='(' || ch=='[')     
                                {
                               ms.push(ch);
                                }
                            else if((ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*')){
                                ms.push(str.charAt(i+1));
                            }

                            else if (ch==')'){
                                if(ms.isEmpty()||ms.pop()!= '('){
                                   System.out.println(") is mismatched");
                                   break loop;
                                }
                            }
                           else if(ch == ']'){
                                if(ms.isEmpty() || ms.pop() != '['){
                                    System.out.println("] is mismatched");
                                    break loop;
                                }      
                           }
                            else if(ch == '}'){
                                if(ms.isEmpty() || ms.pop() != '{'){
                                    System.out.println("} is mismatched");
                                    break loop;
                                }
                            }
                            else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){
                                if(ms.isEmpty() || ms.pop() != '*'){
                                    System.out.println("*/ is mismatched");
                                    break loop;
                                }   
                            }
                           else if(ch == '"'){
                                if(quoteStart == true) {
                                    ms.push(ch);
                                    quoteStart = false;
                               }
                                else {
                                    if(ms.isEmpty() || ms.pop() != '"'){
                                        System.out.println(" \" is mismatched");  
                                        break loop;
                                    }
                                    quoteStart = true;
                                }
                            }  
                       }
                    }
                }
                input.close();
            }
            catch(FileNotFoundException e){
                   System.out.println("Cannot find file");
            }

        }
        else{
               System.out.println("No command line argument");
        }
    }

}

这里我包含一个示例输入文件:

/* this is to test whether the program ignores imbalances in the comment blocks */
public class Test3 {
    public static void main(String[] args) {
        boolean haveYouWatchedHamiltonYet = true;
        int theSchuylerSisters = 3; 
        int alexanderhamilton = 1;
        int aaronburr = 1;

        boolean amIintheroom = theRoomWhereItHappens();


        /* this is a commented block. We're testing if your code 
        can deal with unbalanced things in the comments: /*
        that was the test for here */
    }

    /*just a general comment */ 
    /* this one has some  errors /* { [  { [ } ] */   


    public boolean theRoomWhereItHappens() {
        boolean intheRoomWhereItHappens = false;
        boolean isHappyAboutThis = false;
        return intheRoomWhereItHappens && isHappyAboutThis;

    }
}

最佳答案

您在评论的开头和结尾使用相同的字符:

    int start = str.indexOf("/*");
    int end = str.indexOf("/*");

结束注释字符串应该是:

    int end = str.indexOf("*/");

关于java - 我的程序符合要求,但不打印任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288952/

相关文章:

java - 使用 IP 地址属性的 LDAP 查询

java - 如何添加没有Jar的库

java - 使用 Java StarTeam API,我如何找到 StarTeam 项的哪些修订版具有特定标签?

Java 包和类

java - 对 -fx-alignment : LEFT saying no enum constant javafx. geometry.Pos.LEFT 发出警告

java - 当我在 Android 上扩展应用程序时,上下文在 Glide 上不起作用

java - com.querydsl.core.Tuple 的 ClassCastException

java - 在Spring应用程序中记录容器启动时的静态端点

Java/Spring/JSP - 如何使用 ModelAndView.addObject 输出附加值

java、泛型和 getAll