3

I need some help writing a class that can go through other java files and show the class name, int variable names, and comments.

I have a test class that I'm attempting to parse through here.

public class Test {
    private int x;
    private int y;
    private String s;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // more comments
        int l; //local variable
        l = 0;
    }
}

The output I'm looking to obtain:

The Class name is : Test
There is an int variable named: x
There is an int variable named: y
Comment contains:  TODO Auto-generated method stub
Comment contains:  more comments
There is an int variable named: l
Comment contains: local variable

Here is the code for the class I have now:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

class ExtractJavaParts {

    public static void main(String args[]){

        try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("src/Test.Java");

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line            
            while ((strLine = br.readLine()) != null){
                // Print the content on the console
                if (strLine.contains("class")){
                    System.out.println ("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{")));
                }
                else if (strLine.contains("int")){
                    System.out.println("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";")));
                }
                else if (strLine.contains("//")){
                    System.out.println("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));
                }
            }

            //Close the input stream
            in.close();
        }

        catch (Exception e){
            //Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}

This is the output currently:

The class name is: Test 
There is an int variable named: x
There is an int variable named: y
Comment contains:  TODO Auto-generated method stub
Comment contains:  more comments
There is an int variable named: l

The program as of now will not pick up on comments that occur after code. Any help you can provide to get the desired output is greatly appreciated. Thanks so much!

kmaz13
  • 260
  • 1
  • 5
  • 14

2 Answers2

1

The problem is that in your code you have an int followed by a comment.

When that line is read, it goes into the first "else if" statement and then goes to the next line.

Try using 3 if statements instead of one "if" and two "else if"s

The problem is that for any line, it can go through ONLY ONE conditional statement the way you have it coded. Which means if you have a comment AND an int on the same line, it will only find the int, and then continue to the next iteration of the loop

Steve's a D
  • 3,801
  • 10
  • 39
  • 60
0

The approach will not work very far. You need a parser that understand java source file. You c startan probably start with many open source tools which processes source code. For example -- javancss or checkstyle or javaparser

This answer -- Java : parse java source code, extract methods --has more options

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143