1

我正在开发一个工具来分析和统计其他人的源代码,该工具将能够识别代码中的许多东西!现在我被困在计算代码评论的数量上,我当前的代码是:

 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
/*
*/
4

6 回答 6

5

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

 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 */

确定什么是评论和什么不是评论并不容易:) 我强烈建议您为 java 源寻找开源解析器解决方案。

于 2016-02-18T20:34:31.483 回答
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);
}
}
于 2019-01-21T06:53:40.793 回答
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);
}
}
于 2019-01-21T10:08:06.787 回答
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);
}

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

于 2016-02-18T20:18:55.173 回答
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);
}

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

于 2016-02-18T20:14:28.057 回答
0

伙计们,这是一个简单的解决方案。只需从此链接下载适用于 windows 的 cloc 软件。该软件支持每种语言,也可以接受文件文件夹。将您的文件夹和 cloc 放在同一个地方并打开 cmd 输入此命令

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

并在代码中有行数、空行和总行数。

结果:

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

于 2016-05-27T11:34:59.973 回答