aspose file tools*
The moose likes Java in General and the fly likes How to ignore or remove comments from a java file with the help of another java file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to ignore or remove comments from a java file with the help of another java file." Watch "How to ignore or remove comments from a java file with the help of another java file." New topic
Author

How to ignore or remove comments from a java file with the help of another java file.

Sandeep Koney
Greenhorn

Joined: Dec 02, 2010
Posts: 6
Hi All,

I need some help in java. Can someone help me how to ignore or remove comments from a java file with the help of another java file.

To explain more better, I have a java file which includes single line and multi line comments. I am writing an utility to read the java file but I don’t want the comments to be read.

Can someone throw some light on this issue.

Thanks & Regards,
Sandeep
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39


Shouldn't be too hard. Java only have two comment types, and they don't nest. So...

If you encounter the single line type while parsing, then just read and ignore everything til the end of the current line.

If you encounter the multiline type while parsing, then just read and ignore everything, including end of lines, til you reach the end of comment marker.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sandeep Koney
Greenhorn

Joined: Dec 02, 2010
Posts: 6
Hi Henry,

Thanks for the response. Can you please help me how to identify the end of comment marker while reading the file using readLine().
This is where i stuck.
Thanks in Advance.

Regards,
Sandeep
Sunil Tiwari
Ranch Hand

Joined: Sep 19, 2006
Posts: 49

Hey Sandeep,

You can try using regular expressions to look for comment marker from the readLine().

Another approach you can try would be using String functions.

Remember, to take care of lesser/excessive begin / end comment markers to make you code more efficient

Sunil


~ Neil (SCJP)
Sandeep Koney
Greenhorn

Joined: Dec 02, 2010
Posts: 6
Hi Sunil,

Can you post some sample code for this as i am very much new to regular expressions.
TIA

Regards,
Sandeep
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Java™ Tutorials about regular expressions.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Note that it's possible for Java code to contain the character sequences that start comments -"//" and "/*"- as part of string constants - in which case they do not start comments.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
What you actually want is a parsing tutorial.
Glenn Strickland
Greenhorn

Joined: Dec 09, 2010
Posts: 1
I wrote a method to remove comments from java code in order to compare two programs. There is a regular expression that can do this but I didn't know it at the time. This method does not ignore 'comment' strings inside quoted strings, as it should, but it was good enough for my purpose:
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I'm a fan of using lexers for parsing text (using JFlex, for example), and I think this problem would be a perfect candidate for one. It'd also make a good introduction on how to create lexers since it's not a very involved problem. The result would be easier to maintain -I'd argue that the above code is not very maintainable by someone who didn't write it- and it would be much easier to extend (in case the underlying parsing becomes more complex, as -over time- is usually the case).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to JavaRanch Glenn Strickland
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to ignore or remove comments from a java file with the help of another java file.
 
Similar Threads
don't like to hijack or do i ....
To count the number of lines present under conditional statements(if, if, else if, else)
Cyclomatic Complexity
Lines lost during reading of a file in JTextPane
Cyclomatic Complexity