Home > database >  A java code that identifies the loops and methods in an input text file
A java code that identifies the loops and methods in an input text file

Time:06-15

I am having trouble with writing a successful code that identifies the loops (for loops, while loops, nested for loops) in an input text file(this input was taken using BufferedReader), and methods, where the output should look like this:

(for loop from line x to line x)

(while loop from line x to line x)

(method xxx from line x to line x)

I started by creating a folder that contains a text file that has a random code written in it( in my case it was a bubble sort) the code had comments, so I deleted all comments using replaceAll regex, the output with no comments is written to another .txt file, then, I used BufferedReader to read the new txt file and stored it in an ArrayList, this is where I got stuck, I tried several if statements in order to find for loops as a start. My question is how can I get the desired output I wrote above?

   package assignment1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class tryq3 {
    public static void main(String a[]) throws Exception {
        String fileName = "C://myFolder//output.txt";
        Question1 DDD = new Question1();
        String source = DDD.readFile(fileName);
        String sourceNoBrackets = source.replaceAll("\\(.*\\)", "");
        
        File file1 = new File("C:\\myFolder\\cleanOutput1.txt");
        FileWriter fw = new FileWriter(file1);
        PrintWriter pw = new PrintWriter(fw);
        pw.println(sourceNoBrackets);
        pw.close();
        
        String line;
        int counter = 0;
        ArrayList<String> mylist = new ArrayList();
    
        BufferedReader br = new BufferedReader(new FileReader("C:\\myFolder\\cleanOutput1.txt"));
        while((line = br.readLine())!=null) {
            mylist.add(line);
        }
        
        for(int i=0;i<mylist.size();i  ) {
            int count=0;
            if(mylist.get(i).contains("for")) {
                System.out.println("for loop from "   i);
                
            }
            
            
                
        }
            
            
    }
        
        
        
}

CodePudding user response:

I would recommend that once the word for is found, don't just list the lines but locate where the open bracket { starts and where } is (ends). You can do the same for while loops and a method. You would need to catch cases where there would be a nested for loop; if for then { was found then another for restart the process and first state the for loop inside then the outer. I hope that helps!

for(int i=0;i<mylist.size();i  ) {
        int count=0;
        if(mylist.get(i).contains("for") && mylist.get(i).contains("{")) {
            for(int j=i;j<mylist.size();j  ) {
               if(mylist.get(j).contains("}"){
                   System.out.println("for loop from "   i);
                   i = j; // begin from where j left off
               }
               if(mylist.get(j).contains("for") && mylist.get(j).contains("{"){ // nested loop
                   for(int k=j;k<mylist.size();k  ) {
                        if(mylist.get(k).contains("}"){
                             System.out.println("for loop from "   i);
                             j = k; // begin from where k left off
                        }
                   }
               
               }
            }
            
        }

}
  • Related