0

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++;
       }
    }
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
bentham
  • 1,701
  • 3
  • 20
  • 32
  • Are you only interested in multi-line comments? Are none of the comments going to have anything (including whitespace) before them? Are all 3 "branches" (including the default) supposed to do the same thing? – Matt Whipple Jan 30 '13 at 03:22
  • yes I am interested in multi-line comments, yes just blank spaces, I already have the comments like /* something*/, – bentham Jan 30 '13 at 03:24
  • My suggestion here for logic is trim use a split on the line to tell if the /* is at the begining or end or even middle and associate a boolean function to true and dont count the line until you find the closing tag(do another split to see if there is valid content after the tag. – Sean F Jan 30 '13 at 03:30

3 Answers3

2

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();
        }
    }
  • Let me know if you are looking for something else. –  Jan 30 '13 at 03:37
  • but i want to count comments block comments, this is causing headache – bentham Jan 30 '13 at 03:39
  • Don't worry relax try to apply logic I am sure you will get the solution. I am also applying the logic once i find the solution i will again post my answer my reputation is very low hence i am unable to modify your question just modify question and add. "I want to find the commented line only". put example commented line. –  Jan 30 '13 at 03:44
1

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();
        }
    }

}
0

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 !!