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.