• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing multiline comments

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Code tags


[ December 27, 2005: Message edited by: Stuart Ash ]
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic