Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算java中的注释(单行和多行)?_Java_Comments - Fatal编程技术网

如何计算java中的注释(单行和多行)?

如何计算java中的注释(单行和多行)?,java,comments,Java,Comments,我正在用Java做这个项目。因为我有一个部分,在其中,我必须确定单个、多个注释以及程序中的注释总数。我需要您的指导来计算java中单行注释、多行注释和总无注释行的数量 查看如果您只想了解有关注释和代码的行数摘要,请查看。将其指向源目录,即: cloc src …它将为您显示摘要 CLOC还处理那些使自己很难解决这个问题的极端情况——多行注释、字符串中的注释行等等。以下是: package test; public class Main { /** * A javadoc comment.

我正在用Java做这个项目。因为我有一个部分,在其中,我必须确定单个、多个注释以及程序中的注释总数。我需要您的指导来计算java中单行注释、多行注释和总无注释行的数量

查看

如果您只想了解有关注释和代码的行数摘要,请查看。将其指向源目录,即:

cloc src
…它将为您显示摘要

CLOC还处理那些使自己很难解决这个问题的极端情况——多行注释、字符串中的注释行等等。以下是:

package test;

public class Main {

/**
 * A javadoc comment.
 * @param args
 */
public static void main(String[] args) {
    System.out.println("/* This isn't a comment */");
    /* This is a comment */
    //Comment
    System.out.println("//Not comment");
}
//Comment
}
CLOC给出了2个空行、7个注释行和7个代码行


是的,您可以自己用Java编写一些东西,但除非这是一个特定的家庭作业问题(在这种情况下,它应该被标记为这样),为什么要重新发明轮子呢?您需要处理大量的紧急情况,进行大量测试以确保其工作正常,并将其与现有工具等进行比较。当您已经有了一些已经很好地完成工作的东西时,这样做是没有意义的。

在Java中,您有两种类型的注释:

//-行注释

/**/-阻止注释

基于此,您可以做出简单的假设:

  • 如果行内存在“/”,则此行至少有一条注释

  • 如果行内存在“/*”,则已打开注释块

  • 如果注释块打开(找到),则搜索结束标记“*/”


所以你的任务是检查文件中的每一行是否有这些假设。

这里有一个链接,指向我为同一个问题做的作业。希望这有帮助

注意:这是不完整的,因为它不将java中包含双引号的字符串(在双引号内)的大小写作为代码行,而是将其作为注释。这仍然需要解决

解释如下:

Note :- This program is not optimized for performance, performance is not an attribute i care about here. I chose not to use Pattern.compile() among many other decisions.

I count the empty lines, comment lines and lines of code separately.

if there is any sentence which has a some tabs or just spaces, they are counted as
empty lines. They match with the regular expression \s*
\s - any whitespace character (\n\t\b)
 * - this means there can be any number of whitespace characters


comment lines are the ones that match any of the following conditions :-
1) have a // outside of a string
2) have a /* some comment */
3) have a /* but the '*/' is not found on the same line

The regular expression for the above can be found inside the code!

Lines of code are the ones that end in ')' , '}' , '{' or ';' (one problem with this approach is that it does not count lines that have code followed by comment as a line of code)

The summary of these three types of lines are provided by this program.

在我的一个课堂作业中,我编写了一个简单的java代码来完成这个任务。

我以前创建过这个文件,但最近添加到github。 该程序在一个文件上运行,并逐行运行。它在runtime filename.java上接受一个输入参数

例如,要运行此程序:
java CommentAnalysis filename.java

它统计单行(//)和多行注释(//*…*/)

我试图涵盖中提到的不同评论场景


我也在自述文件中编写了关于我的实现的假设,但根据个人需求进行更改应该不会太困难。请检查并告知我是否遗漏了任何注释场景。

我尝试过,效果很好。使用下面的代码,您必须检查每一行:

                if (isEmpty(s) || s.IndexOf("\"//\"") != -1 || s.IndexOf("\"/*\"") != -1|| s.IndexOf("\"*/\"") != -1) continue;
                if (s.IndexOf("/*") != -1) checkCML = true;
                if (s.IndexOf("*/") != -1) checkCML = false;

                if (s.IndexOf("/*") != -1 && s.IndexOf("*/") != -1) ++commentLine;

                if (s.IndexOf("//") != -1 || checkCML) ++commentLine;

它适用于我。它计算java文件中单行和多行注释的总数

public class CountComment {

    public static void main(String[] args) throws IOException {
        try {
            String str;
            BufferedReader f = new BufferedReader(new FileReader("a.java"));
            LineNumberReader l = new LineNumberReader(f);
            int numberline = 0;
            int count = 0;
            while ((str = l.readLine()) != null) {
                numberline++;
                if (str.startsWith("/*")) {
                    while ((str = l.readLine()) != null) {
                        count++;
                        if (str.endsWith("*/")) {
                            count++;
                            break;
                        }
                    }
                }
                else if(str.startsWith("//")){
                    count++;
                }
            }
            System.out.println("" + count);
        } catch (FileNotFoundException e) {
            System.out.println("file not found");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你试过什么了吗?如果是的话,你在哪里卡住了?@Bart自己这么做是不寻常的,因为unicode转义序列(\u000a或\u002f\u002f)、字符串文本(“http://example.com”)和单行注释(//test/*test)中的多行注释必须解释,可能还有其他一些陷阱。@Christian Semrau,我没有说他/他应该自己做。我只是问他/他已经做了什么。这包括(在做作业的情况下)自己执行这些计算的代码,或者(在“专业”工作的情况下)什么工具和/或API已经尝试过了。你是对的。没有冒犯的意思!你可以看看这个相关的。为了可靠地识别注释,还需要解析语言的某些部分:不要计算字符串文本中的注释,忽略单行注释中多行注释的开头,反之亦然,处理unicode以单行注释结尾的转义序列,如\u000a。必须小心处理您的假设:1)
String s=”//";没有注释,2)行
//single/*line
没有开始块注释,3)序列
*\u002f
结束块注释。cloc是否解释了我在对问题的注释中提到的陷阱?@Christian Semrau是的,我已经用一个例子更新了我的答案。相同的java实现在哪里?您提供的链接有php实现CLOC处理单行注释中开始的多行注释(它正确地忽略它们)。我不处理unicode转义序列,也不处理字符串文本中的块注释。这些都是已知的限制,可以在Cloc文档中看到。@Christian Semrau承认,这不是防弹的。但在大多数情况下,这已经足够好了,需要付出巨大的努力(很可能是不可行的)才能想出更好的办法。这并不能真正回答问题。您应该直接解释它是如何完成的,而不是仅仅链接到其他人稍后遇到此问题时可能不存在的代码示例。您应该在此处解释解决方案,而不是链接到文件。感谢您的评论,我已经添加了一个解释。请告诉我如何使链接文件更永久(有允许我这样做的网站吗?),这样你就不必担心我删除它(只是偶然的)?
public class CountComment {

    public static void main(String[] args) throws IOException {
        try {
            String str;
            BufferedReader f = new BufferedReader(new FileReader("a.java"));
            LineNumberReader l = new LineNumberReader(f);
            int numberline = 0;
            int count = 0;
            while ((str = l.readLine()) != null) {
                numberline++;
                if (str.startsWith("/*")) {
                    while ((str = l.readLine()) != null) {
                        count++;
                        if (str.endsWith("*/")) {
                            count++;
                            break;
                        }
                    }
                }
                else if(str.startsWith("//")){
                    count++;
                }
            }
            System.out.println("" + count);
        } catch (FileNotFoundException e) {
            System.out.println("file not found");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}