计算java中的注释数量

时间:2016-02-18 20:09:11

标签: java cloc

我正在开发一个工具来分析并提供有关其他人的源代码的一些统计信息,该工具将能够识别代码中的许多内容!现在我很难计算代码的评论数量,我目前的代码是:

 public static void main(String[] args) {

    String line = "";
    int count = 0;
    try {
        BufferedReader br = new BufferedReader(new FileReader("comments.txt"));
        while ((line = br.readLine()) != null) {
            if (line.startsWith("//")) {
                count++;
            } else if (line.startsWith("/*")) {
                count++;
                while (!(line = br.readLine()).endsWith("'*\'")) {
                    count++;
                    break;
                }
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("count=" + count);
}

要检查代码,我正在使用测试文件。但是代码在两个文件中都给出了错误的结果,例如;我在以下文件中得到三个

Yes
//comment
yes
yes
/*
if
random
test
test
*/

虽然答案应该是两条评论!

在下面的文件中,它显示我有五条评论,而我实际上还有两条

Yes
//comment
yes
yes
/*
if
random
test
test
/*
*/

6 个答案:

答案 0 :(得分:5)

整个方法都存在缺陷。您需要正确解析源文件,至少需要正确跟踪引号和#34; / *"的嵌套。请注意,任何注释字符组合都可以出现在以下语句中:

 System.out.println("// this is *not* a line comment");
 String s = "*/ this is not the end of a block comment";

等等。然后,在解释文件之前处理字符转义序列会产生奇怪的行为:

    \u002F* this is a valid comment */

答案 1 :(得分:0)

我还没有测试过您的代码,但我相信这应该可行:

public static void main(String[] args) {

    String line = "";
    int count = 0;
    try {
        BufferedReader br = new BufferedReader(new FileReader("comments.txt"));
        while ((line = br.readLine()) != null) {
            if (line.startsWith("//")) {
                count++;
            } else if (line.startsWith("/*")) {
                count++;
                while ((line = br.readLine())!=null && !line.endsWith("'*\'"));
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("count=" + count);
}

当您遇到/*时,您应该递增计数器并跳过评论部分。

答案 2 :(得分:0)

我认为你有一个问题,即评论可以在一行内部或末尾发生......

public static void main(String[] args) {

    String line = "";
    int count = 0;
    try {
        BufferedReader br = new BufferedReader(new FileReader("comments.txt"));
        while ((line = br.readLine()) != null) {
            if (line.contains("//")) {
                count++;
            } else if (line.contains("/*")) {
                count++;
                while (!line.contains("*/") && !(line = br.readLine()).contains("*/"));
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("count=" + count);
}

当然这里的问题是如果“//”,“/ *”或“* /”序列出现在引用文本中....?

答案 3 :(得分:0)

这里的人是一个简单的解决方案。只需从这个link for windows下载cloc软件即可。 该软件支持所有语言和也可以接受文件夹。将您的文件夹和cloc放在同一个地方并打开cmd键入此命令

cloc-(version no).exe (folder name)
cloc-1.64.exe main

并且代码中没有行,空白行和总行数。

RESULTS:

有关详细信息,请参阅:http://cloc.sourceforge.net/

答案 4 :(得分:0)

    enter code here
public class FilterInputStreamDemo {
public static void main(String[] args) {

    String line = "";
    int comment_count = 0;
 int line_count = 0;
int single_comment_count = 0;
int multiple_comment_count = 0;
    try {
        BufferedReader br = new BufferedReader(new FileReader("comments.txt"));
        while ((line = br.readLine()) != null) {
    line_count++;
            if (line.startsWith("//")) {
                comment_count++;
        single_comment_count++;
            } else if (line.startsWith("/*")) {
                comment_count++;
        multiple_comment_count++;
                while (!(line = br.readLine()).endsWith("'*\'")) {
                    comment_count++;
            multiple_comment_count++;
                    break;
                }
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("comment_count=" + comment_count);
}
}

答案 5 :(得分:0)

package com.usaa.training;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CommentsReading {
public static void main(String[] args) {

    String line = "";
    int number_of_blocks = 0;
    int comment_count = 0;
 int line_count = 0;
 int TODO = 0;
int single_comment_count = 0;
int multiple_comment_count = 0;
    try {
        File file = new File("C:\\code\\InvolvedPartyBasicInfoMapper.java");
        BufferedReader br = new BufferedReader(new FileReader(file));
        while ((line = br.readLine()) != null) {
    line_count++;
            ;
            if (line.contains("//")) {
                 if (line.contains("TODO")){
                     TODO++;
                 }
                comment_count++;
        single_comment_count++;
            } else if (line.contains("/*") ) 
            {
                if (line.contains("TODO")){
                 TODO++;
             }
                comment_count++;
        multiple_comment_count++;
      if (line.endsWith("*/"))
      {
          break;
      }

                while (!(line = br.readLine()).endsWith("'*/'") )
                {
                    line_count++;
                    comment_count++;
            multiple_comment_count++;
            if (line.endsWith("*/"))
            {
                number_of_blocks++;
                break;
            }


                }
            }
        }
        br.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Total # of Lines  = " + line_count);
    System.out.println("Total # of Comment Lines= " +comment_count);
 System.out.println("Total # of Single line Comments= " +single_comment_count );
 System.out.println("Total # of Comment lines with Block Comments = " +multiple_comment_count );

    System.out.println("Total # of Block line Comments = " +number_of_blocks);

    System.out.println("No of TODO's  = " +TODO);
}
}