1

I'm developing a tool that recognizes test classes and counts the number of lines in those classes. The tool will also count the lines of the business code and compare the both results, here is my code:

    for (File f : list) {
        if (f.isDirectory()) {
            walk(f.getAbsolutePath());
        }

        if (f.getName().endsWith(".java")) {

            System.out.println("File:" + f.getName());
            countFiles++;

            Scanner testScanner = new Scanner(f);
            while (testScanner.hasNextLine()) {

                String test = testScanner.nextLine();
                if (test.contains("org.junit") || test.contains("org.mockito")) {
                    System.out.println("this is a test class");
                    testCounter++;

                    break;
                }
            }
            Scanner sc2 = new Scanner(f);

            while (sc2.hasNextLine()) {

                count++;

counting the business code using (count++) is working perfectly, but counting the number of code in test classes is not working using (testCounter++) is returning the number of test classes rather than the number of lines in those classes! what can I do ?

Thanks

user5923402
  • 217
  • 3
  • 12

1 Answers1

1

Assuming that you are wanting to count the number of lines that contain either org.junit or org.mockito

then you want to do

       while (testScanner.hasNextLine()) {

            String test = testScanner.nextLine();
            if (test.contains("org.junit") || test.contains("org.mockito")) 
            {
                hasTestLines = true;
            }
            count++;
        }

        if (hasTestLines) {
             System.out.println(String.format ("there were %d lines in file %s which also had org.junit||org.mockito", 
                                    count, f.getName());
        }
        else {
             System.out.println(String.format ("there were %d lines in file %s which did NOT have org.junit||org.mockito", 
                                    count, f.getName());
       }
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • no actually not! I want to count the number of LINES IN each CLASS (the whole class) containing "org.mockito" or "org.junit" not – user5923402 Feb 29 '16 at 04:44
  • What does your comment mean? See my edit, maybe this is what you want? – Scary Wombat Feb 29 '16 at 04:48
  • your solution is assuming that i want to count the number of lines that contains org.mockito or org.junit. But I actually want to count the number of lines in any class that has a line that may contain org.mockito or org.junit – user5923402 Feb 29 '16 at 04:52