user2737948
user2737948

Reputation: 339

Count the lines of several methods but return separate values not the sum of all

I'm going to elabortate my question because I had a hard time labeling the question the right way.

I'm labeling my methods like this:

 /*Start*/
  public void contadorDeLineas(File file)throws IOException{
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
/*Finish*/

I have several methods labeled like that but I want to count them separately, but the code I wrote counts the lines inside the methods starting from public and finishing in "Finish". But as for now the code I wrote counts all the lines inside the methods and return the sum of all the lines. What I want to do is read the first block return that value and continue searching for the next block of code. This is the code I wrote

public void LinesMethods(File file)throws IOException{
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int lines_inside = 0;

while((line = br.readLine())!=null){
  if(line.startsWith("/*St")){
    do{
      line = br.readLine();
      line = line.trim();
      System.out.println(line);
      if(!(line.equals("") || line.startsWith("/*"))){
        lines_inside++;
      }
    }while(!line.startsWith("/*Fi"));
  }
}
br.close();
System.out.println("Found: " + lines_inside);
}

This is an example of what my code is showing in the console

/*Start*/
public void LineMethods(File file)throws IOException{
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
/*Finish*/
/*Start*/
public static void main(String[] args)throws IOException{
program2 test = new program2();
File root = new File(args[0]);
test.LOC(root);
System.out.println("Found: " + test.lines);
System.out.println("Other type of lines: " + test.toDo);
}
}
/*Finish*/
Block comments lines: 11

Instead I'm looking for a result like a first print showing the number 3 and then a number 8. Any guidance will be appreciated.

Upvotes: 0

Views: 67

Answers (3)

Eritrean
Eritrean

Reputation: 16498

if you have several methods printing out all the lines may be to much when you are only interested in the number of lines. You can put the names of the methods and the number of lines in a map and print that out.

public static void LinesMethods(File file)throws IOException{
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    int lines_inside = 0;

    Map<String, Integer> map = new HashMap<>();

    while((line = br.readLine())!=null){
        lines_inside = 0;
      if(line.startsWith("/*St")){
          String method = "";
        do{
          line = br.readLine();

          if(line.contains("public")){
              method = line.substring(0, ine.indexOf('('));                  
          }

          line = line.trim();

          if(!(line.equals("") || line.startsWith("/*"))){
            lines_inside++;
          }
        }while(!line.startsWith("/*Fi"));
        map.put(method, lines_inside);
      }
    }
    br.close();
    for(String s :map.keySet()){
        System.out.println(s +" : "+ map.get(s));
    }
}

Upvotes: 1

user4910279
user4910279

Reputation:

Try this

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        boolean inside = false;
        int count = 0;
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains("/*Start*/")) {
                inside = true;
                count = 0;
            } else if (line.contains("/*Finish*/")) {
                System.out.println("Found: " + count);
                inside = false;
            } else if (inside) {
                ++count;
            }
        }
        if (inside && count > 0)
            System.out.println("Found: " + count);
    }

Upvotes: 1

Abdelhak
Abdelhak

Reputation: 8387

Try to put lines_inside++; in the code like this:

  while((line = br.readLine())!=null){
    lines_inside++;
    ...

This gives you the rught number of elements in file.

Upvotes: 0

Related Questions