Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count comment (single & multiple) lines in java?

Tags:

java

comments

I am doing project in Java. In that I am having one part in which, I have to identify the single, multiple comments and total no of comments in the program. I am in need of your guidance to count the no of single line comment, multiple line comment and total no comment lines in java.

like image 633
Madhushudhanan Avatar asked Mar 12 '11 14:03

Madhushudhanan


2 Answers

If you just want a summary of the line count with regards to comments and code, look at CLOC. Point it at a source directory, i.e.:

cloc src

...and it'll display a summary for you.

CLOC also deals with the corner cases that make it difficult to do this problem yourself - multiline comments, comment looking lines within strings and so on. The following:

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 gives 2 blank lines, 7 comment lines, and 7 code lines.

Yes, you could write something yourself in Java but unless this is a specific homework question (in which case it should be tagged as such) why reinvent the wheel? You'd need to deal with lots of corner cases, do lots of tests to make sure it worked, compare it against existing tools etc. and there's really no point in doing that when you've got something that does the job well already.

like image 149
Michael Berry Avatar answered Oct 11 '22 02:10

Michael Berry


Here's a link to an assignment that I did for the same question. Hope this helps.

Note: It is not complete, cos it does not count the case of strings in java(within double quotes) that contain double quotes preceded by escape characters(like "/\"/") as line of code, but rather it counts it as a comment. This still has to be resolved.

http://bit.ly/PDasu9

Here is the explanation :-

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.
like image 35
wlan0 Avatar answered Oct 11 '22 02:10

wlan0