How to count comments (single and multiple) lines in java?

I am doing a Java project. That I have one part in which I have to identify single, multiple comments and the total number of comments in the program. I need your guide to count the number of comments of a single line, several line comments and the total number of comment lines in java.

+3
source share
6 answers

If you just need a summary of the number of lines for comments and code, check out CLOC . Direct it to the source directory, i.e.:

cloc src

... and it will display a summary for you.

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 , 7 7 .

, - Java, ( ), ? , , , , .., , -, .

+3

, . , .

. , java ( ), , escape- (, "/\" /") , . .

http://bit.ly/PDasu9

: -

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.
+2
0

Java :

//-

/* */- .

, :

  • "//", .

  • "/*", .

  • (), "*/".

, - .

0
//Below is the working code 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CountComment {

    public static void main(String[] args) {
        String line="";
        int count=0;
        try {
            BufferedReader br=new BufferedReader(new FileReader("./src/numbers/comment.txt"));
                while((line=br.readLine())!=null)
                {
                    if(line.startsWith("//"))
                        count++;
                    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);
        } 


    }
0

Java-, . https://github.com/chetanpawar0989/CommentAnalysis

, github. . imput filename.java

, : java CommentAnalysis filename.java

(//), (/*... */).

, .

readme, . , , .

0
source

All Articles