how to count the loc without block comments?

1.9k Views Asked by At

I am trying to get the number of lines of code from a java file. But I am having trouble counting them.

First I tried to skip them with ifs, but my idea does not work. Now I am counting the same lines with comments, my Java file has this header. Any ideas, I am stuck in how to count them.

My if is for getting the number of lines with the comments block. I trying to make a subtract.

/*
example
example
*/

int totalLoc = 0;
int difference = 0;
while((line =buff.readLine()) !=null){

    if((line.trim().length() !=0 &&(!line.contains("/*") ||!line.contains("*/")) )){
       if(line.startsWith("/*")){
           difference++;
       }else if(linea.startsWith("*/")){
           difference++;
       }else{
           difference++;
       }
    }
}
3

There are 3 best solutions below

3
On BEST ANSWER

If you want to count lines in any file write below method and pass the fileName as input to below method and it will return counts.

public int count(String filename) throws IOException
     {
        InputStream is = new BufferedInputStream(new FileInputStream(filename));
        try
        {
            byte[] c = new byte[1024];
            int count = 0;
            int readChars = 0;
            boolean empty = true;
            while ((readChars = is.read(c)) != -1)
            {
                empty = false;
                for (int i = 0; i < readChars; ++i)
                {
                    if (c[i] == '\n')
                        ++count;
                }
            }
            return (count == 0 && !empty) ? 1 : count;
        }
        finally
        {
            is.close();
        }
    }
1
On

Got the solution try below code it will print all multiline comments as well as total lines of multiline comments found in a file.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.charset.Charset;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

public class LinesOfCode {
    public static void main(String[] args) {
        try {
            String s = readFile("D:\\src\\SampleClass.java");

            Pattern p = Pattern.compile("/\\*[\\s\\S]*?\\*/");

            Matcher m = p.matcher(s);

            int total = 0;
            while (m.find()) {

                String lines[] = m.group(0).split("\n");
                for (String string : lines) {
                    System.out.println(string);
                    total++;
                }
            }
            System.out.println("Total line for comments = " + total);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String readFile(String path) throws IOException {
        FileInputStream stream = new FileInputStream(new File(path));
        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
                    fc.size());
            /* Instead of using default, pass in a decoder. */
            return Charset.defaultCharset().decode(bb).toString();
        } finally {
            stream.close();
        }
    }

}
1
On

http://ostermiller.org/findcomment.html check out this link it will help you more. and by using this expression => (/*([^]|[\r\n]|(*+([^/]|[\r\n])))*+/)|(//.) you can count both comments single line and multi line !!