Hi all,,
how can i ignore the comment statements that begin with "/* " and ends with
" * / "
for example: /* the class is/
or
/
problem is
very difficult */
i want to remove these statement when i reading java file line by line

package filename1;
public class filename1{
public static void main(String[]argv) throws ClassFormatException, IOException {

 try {
            fileName = "C:\\NetBeansProjects\\filename\\src\\filename\\filename.java";
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);

            line = br.readLine();

            while (line !=null) {
             for( int i=0;i<line.length();i++)
                 {            
                  b=line.indexOf("/",i);
                  ee=line.indexOf("*",i);
                    if(b!=-1 && ee!=-1)

                    v=line.indexOf("*/",i);
                      if (v==-1 )

                 line=" ";
                  }



            System.out.println(line);
            line = br.readLine(); 

        }

        }
        catch (IOException e) {
         e.printStackTrace();
        }}}

...can anyone help me >>whats wrong in my program??

Edited 9 Months Ago by Mar. Na.

my recommendation: don't.

even though /* and */ are usually (not always!!) used for comments, some frameworks (GWT, for instance) use it to tell you that native code is being used.

Here is a simple example of how to code a JSNI method that puts up a JavaScript alert dialog:

public static native void alert(String msg) /*-{
  $wnd.alert(msg);
}-*/;

so, by 'removing the comments' you'll actually be changing the functionality of the application. In this case, all that's lost is a message, but what if this code is supposed to calculate results to be used later?
Still want to do it ... well, my guess, looking for indexOf("*/") instead of "/" should be able to help you out.

Don't forget that /* can appear inside a line comment, and thus may not have a matching */

//* this is a valid Java comment without a closing delimiter

And /* or // can appear inside a String constant

String s = "/* this is not a comment"; /* but this is */
String s = "// neither is this"; // but this is

so before you process /* ... */ you need to ignore any delimiters that are inside a String constant, then remove everything that appears after a //

For the definitive rules on comments see the Java Language Spec
https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.7

As for "what's wrong with my program" - I see at least three major errors in the loop, in fact it's complete nonsense. Inserting some print statements will allow you see what it is really doing, and why/where its wrong.

Edited 9 Months Ago by JamesCherrill

Thank you very very much......it's really good website to help...

Edited 9 Months Ago by Mar. Na.

This article has been dead for over six months. Start a new discussion instead.