0

我使用以下代码来计算代码中的注释数:

StringTokenizer stringTokenizer = new StringTokenizer(str);
int x = 0;

while (stringTokenizer.hasMoreTokens()) {
    if (exists == false && stringTokenizer.nextToken().contains("/*")) {
        exists = true;

    } else if (exists == true && stringTokenizer.nextToken().contains("*/")) {

        x++;
        exists = false;

    }
}

System.out.println(x);

如果评论有空格,它会起作用:

例如:"/* fgdfgfgf */ /* fgdfgfgf */ /* fgdfgfgf */"

但它不适用于没有空格的评论:

例如:"/*fgdfgfgf *//* fgdfgfgf*//* fgdfgfgf */"

4

3 回答 3

4

在commons lang中使用 StringUtils 类,您可以非常轻松地归档它

    String str = "Your String"
    if (&& StringUtils.countMatches(str,"/*") != 0) {
       //no need this if condition
    } else if (StringUtils.countMatches(str,"*/") != 0) {
         x = StringUtils.countMatches(str,"*/");
    }
    System.out.println(x);
于 2013-07-28T14:12:56.547 回答
0

new StringTokenizer(str,"\n")标记/拆分str成行,而不是使用默认分隔符,即\t\n\r\f空格、制表符、换页符、回车符和换行符的组合

StringTokenizer stringTokenizer = new StringTokenizer(str,"\n");

这将换行符指定为用于标记化的唯一分隔符

使用您当前的方法:

String line;
while(stringTokenizer.hasMoreTokens()){

 line=stringTokenizer.nextToken();

   if(!exists && line.contains("/*")){
        exists = true;
   }
   if(exists && line.contains("*/")){
        x++;
        exists = false;
 }    
}

对于多条评论,我尝试使用/\\*&\\*/作为模式split()并获取它们在字符串中出现的长度,但不幸的是,由于拆分不均匀,长度并不准确。

多条/单条评论可以是:(IMO)

COMMENT=/* .* */
A = COMMENT;
B = CODE;
C = AB/BA/ABA/BAB/AAB/BAA/A;
于 2013-07-28T13:58:00.093 回答
0

这让我想起了 Ruby/Perl/Awk 等人的触发器。无需使用StringTokenizer. 你只需要保持状态来计算带有注释的行数。

  1. 您在评论块内。您开始打印或收集所有字符。一旦你遇到一个*/完整的你切换评论块开关。并切换到状态 2

  2. 你拒绝一切,直到你遇到 a/*并回到状态 1。

像这样的东西

public static int countLines(Reader reader) throws IOException {
    int commentLines = 0;

    boolean inComments = false;
    StringBuilder line = new StringBuilder();

    for (int ch = -1, prev = -1; ((ch = reader.read())) != -1; prev = ch) {
        System.out.println((char)ch);
        if (inComments) {
            if (prev == '*' && ch == '/') { //flip state
                inComments = false;
            }

            if (ch != '\n' && ch != '\r') {
                line.append((char)ch);
            } 

            if (!inComments || ch == '\n' || ch == '\r') {
                String actualLine = line.toString().trim();
                //ignore lines which only have '*/' in them
                commentLines += actualLine.length() > 0 && !"*/".equals(actualLine) ? 1 : 0;
                line = new StringBuilder();
            }
        } else {
            if (prev == '/' && ch == '*') { //flip state
                inComments = true;
            }
        }
    }

    return commentLines;
}

public static void main(String[] args) throws FileNotFoundException, IOException {
    System.out.println(countLines(new FileReader(new File("/tmp/b"))));
}

上面的程序忽略空行注释或仅包含/**/其中的行。我们还需要忽略字符串标记器可能无法执行的嵌套注释。

示例文件 /tmp/b

#include <stdio.h>
int main()
{
    /* /******* wrong!  The variable declaration must appear first */
    printf( "Declare x next" );
    int x;

    return 0;
}

返回 1。

于 2013-07-28T18:41:43.510 回答