1

i am writing a code where i want to print only comments in a java file , it worked when i have a comments like this

// a comment

but when i have a comment like this :

// /* cdcdf

it will not print "/* cdcdf" , it only prints a blank line anyone know why this happens ?

here is my code :

package printC;

import java.io.*; 
import java.util.StringTokenizer;
import java.lang.String ;


public class PrintComments {
    

    public static void main(String[] args) {
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(args[0]));
            while ((line = br.readLine()) != null) {
                if  (line.contains("//") ) {
                     StringTokenizer st1 =  new StringTokenizer(line, "//"); 
                     if(!(line.startsWith("//"))) {
                         st1.nextToken();
                     }
                     System.out.println(st1.nextToken()); 
                }
            }   
        }catch (Exception e) {
            System.out.println(e);
        }       
    }
}
0

4 Answers 4

1

You can simplify the code by just looking for the first position of the //. indexOf works fine for this. You don't need to tokenize as you really just want everything after a certain position (or text), you don't need to split the line into multiple pieces.

If you find the // (indexOf doesn't return -1 for "not found"), you use substring to only print the characters starting at that position.

This minimal example should do what you want:

import java.io.*;
import java.util.StringTokenizer;


public class PrintComments {
    public static void main(String[] args) throws IOException {
        String line;    // comment
        BufferedReader br = new BufferedReader(new FileReader(args[0]));
        while ((line = br.readLine()) != null) {
            int commentStart = line.indexOf("//");
            if (commentStart != -1) {
                System.out.println(line.substring(commentStart));
            }
        } // /* that's it
    }
}

If you don't want to print the //, just add 2 to commentStart.

Note that this primitive approach to parsing for comments is very brittle. If you run the program on its own source, it will happily report //"); as well, for the line of the indexOf. Any serious attempt to find comments need to properly parse the source code.

Edit: If you want to look for other comments marked by /* and */ as well, do the same thing for the opening comment, then look for the closing comment at the end of the line. This will find a /* comment */ when all of the comment is on a single line. When it sees the opening /* it looks whether the line ends with a closing */ and if so, uses substring again to only pick the parts between the comment markers.

import java.io.*;
import java.util.StringTokenizer;


public class PrintComments {
    public static void main(String[] args) throws IOException {
        String line;    // comment
        BufferedReader br = new BufferedReader(new FileReader(args[0]));
        while ((line = br.readLine()) != null) {
            int commentStart;
            String comment = null;

            commentStart = line.indexOf("//");
            if (commentStart != -1) {
                comment = line.substring(commentStart + 2);
            }

            commentStart = line.indexOf("/*");
            if (commentStart != -1) {
                comment = line.substring(commentStart + 2);
                if (comment.endsWith("*/")) {
                    comment = comment.substring(0, comment.length() - 2);
                }
            }

            if (comment != null) {
                System.out.println(comment);
            }
        } // /* that's it
        /* test */
    }
}

To extend this for comments that span multiple lines, you need to remember whether you're in a multi-line comment, and if you are keep printing line and checking for the closing */.

Sign up to request clarification or add additional context in comments.

1 Comment

thats amazing , i am actually changing all of my code to do what you said , but i have a little problem , in my code i also can get a comment like this /* comment / , i figured how to find / and look if it appears before // , but the problem now is how to not print */ at the end
1

StringTokenizer takes a collection of delimiters, not a single string delimiter. so it is splitting on the '/' char. the "second" token is the empty token between the two initial "//".

If you just want the rest of the line after the "//", you could use:

if(line.startsWith("//")) {
  line = line.substring(2);
}

2 Comments

how can i split on the "//" string ? because this code is not complete i just put a little part of it to understand the problem , i actually need to split on "//" because sometimes there are comments who starts like this " */ "
i didnt understand this solution . it is not working ..
0

Additional to @jtahlborn answer. You can check all of the token by iterating token: e.g:

...
 StringTokenizer st1 =  new StringTokenizer(line, "//"); 
 while (st1.hasMoreTokens()){
        System.out.println("token found:" + st1.nextToken());
}
...

4 Comments

how can i split on the "//" string ? because this code is not complete i just put a little part of it to understand the problem , i actually need to split on "//" because sometimes there are comments who starts like this " */ "
do you really want to split or just want to get the remaining string after '//' ?
i want the remaining
but sometimes i have */ so if it splits only of * thin there is a problem
0

If you are reading per line, the StringTokenizer don't do much in your code. Try this, change the content of if like this:

if(line.trim().startWith("//")){//true only if líne start with //,aka: comment line
//Do stuff with líne
String cleanLine = line.trim().replace("//"," ");//to remove all // in line
String cleanLine = línea.trim().substring(2,línea.trim().lenght());//to remove only the first //
}

Note: try to always use the trim() to remove all Blanc spaces at begin and end of string.

To split the líne per // use: líne.split("//")

For more general purpose,check out :

Java - regular expression finding comments in code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.