aspose file tools*
The moose likes Beginning Java and the fly likes Removing multiline comments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing multiline comments " Watch "Removing multiline comments " New topic
Author

Removing multiline comments

nick angel
Ranch Hand

Joined: Dec 14, 2005
Posts: 39
Hello to everyone. I am having a little trouble with removing multiline comments from a file I am reading. In the original file, we have occureances of the sort:

a)
/* ...............
.............*/ text to keep

b)
......./*
.......
.......*/

As I read line by line in a file (we are asked to use bufferedreaders), I do the following:

public Vector readFile(File inFile) throws IOException {
Vector lines = new Vector();
String line = "";

BufferedReader bi = new BufferedReader(new FileReader(inFile),
BUFFER_SIZE);

while ((line = bi.readLine()) != null) {
line = this.initialLineFormat(line);
line = this.filterLine(line);

if (line == null) {
continue;
}

if (this.flag == true) {
lines.add(line);
}
}

for (int i = 0; i < lines.size(); i++) {
String currentLine = (String) lines.get(i);
System.out.println(currentLine);
}
bi.close();

return lines;
} // end of readFile method

private String initialLineFormat(String currentLine) {
currentLine = currentLine.replaceAll("\t", "");
return currentLine.trim();
}

private String filterLine(String currentLine) {

if (currentLine.indexOf(NEW_OPERATOR) != -1) {
return null;
}
// /* is located
if (currentLine.indexOf(MULTILINE_START_DELIMITOR) != -1) {
return this.multiLineComment(currentLine);
}
// */ is located
if (currentLine.indexOf(MULTILINE_END_DELIMITOR) != -1) {
return this.multiLineComment(currentLine);
}

return currentLine;

}

private String multiLineComment(String currentLine) {
if (currentLine.indexOf(MULTILINE_START_DELIMITOR)==0) {
this.flag = false;
return null;
} else if (currentLine.indexOf(MULTILINE_START_DELIMITOR) > 0) {
this.flag = false;
return currentLine.substring(0,
currentLine.indexOf(
MULTILINE_START_DELIMITOR) - 1);

} else if (currentLine.indexOf(MULTILINE_END_DELIMITOR) != -1) {
this.flag = true;
return currentLine.substring(currentLine.indexOf(
MULTILINE_END_DELIMITOR) + 1, currentLine.length() - 1);
} else {
return null;
}
}

where flag is a class variable.

When I run this code I only retrieve certain values. For example, in the following scenario:

.........../*
..........*/

both lines are eliminated. Would anyone have a suggestion of a better method of leaving out the multiline comments. I believe the problem I am having is with the instance variable flag that eliminates an additional line, but have not found a solution yet.

Thank you for any help.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
In Code tags


[ December 27, 2005: Message edited by: Stuart Ash ]

ASCII silly question, Get a silly ANSI.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Tried reposting your code in code tags for readability, didn't seem to work. Anyways, I would approach it like this:



This is somewhat pseudocodish. I hope the moderators won't delete
my post because I am posting solution code. I am not spoonfeeding,
I am just posting code because the asker has already done some coding himself.

I am sure there are bugs in this code, but an approach like this
might be simpler. I found it straining to read your code (besides,
I am a little lazy/busy to read every line of it)

And yes, I am using http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html
[ December 27, 2005: Message edited by: Stuart Ash ]
nick angel
Ranch Hand

Joined: Dec 14, 2005
Posts: 39
I am extremely sorry for the lousy formatting of my post. However I thank you for your quick response and I will begin working on my problem immediately.

Thank you.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by nick angel:
I am extremely sorry for the lousy formatting of my post. However I thank you for your quick response and I will begin working on my problem immediately.

Thank you.


No probs and you're welcome. Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing multiline comments
 
Similar Threads
FTP Socket Programming
Incompatible type issue
Confuse BufferedReader type with String
How can I create an object based on input
Send a String to Server and server send it somewhere on network