Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandeep Koney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sandeep Koney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Clojure Redhat Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Koney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48946
60
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you actually want is a parsing tutorial.
 
Glenn Strickland
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Glenn Strickland
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic