This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How Do I Ignore Comments?  RSS feed

 
Mulyadi Kurniawan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code:
import java.io.*;
import java.util.*;
public class TextFileInputTest {
public static void main(String[] args) {
String[][] statData = new String[20][3];
int rowNumber = 0;
try { // To open the file and read out data.
FileReader fReader = new FileReader("stat.txt");
BufferedReader bReader = new BufferedReader(fReader);
String s = "";
try { // To read all the data and store in the data array.
while (s != null) { // End of file when s = null.
s = bReader.readLine();
// Sometimes there are trailing blank lines in files.
if ((s != null) && !s.equals("") && !s.equals(" ")) {
StringTokenizer t = new StringTokenizer(s, ";");
for(int j = 0; j < 3; j++) {
try
{
statData[rowNumber][j] = t.nextToken();
}
catch (ArrayIndexOutOfBoundsException exc)
{
System.err.println("The data array is not in the right format. Check your stat file.");
System.err.println(exc);
}
}
rowNumber++;
}
}
fReader.close();
}
catch (IOException exc) {
System.err.println("Cannot read the stat file.");
System.err.println(exc);
}
}
catch (FileNotFoundException exc) {
System.err.println("Cannot find the stat file.");
System.err.println(exc);
}
// Echo back to see if we got all the data.
for(int i = 0 ; i < rowNumber ; i++) {
System.out.print(statData[i][0] + ", ");
System.out.print(statData[i][1] + ", ");
System.out.println(statData[i][2]);
}
}
}
=======
And this is my stat.txt file:
/*
This is the stat file
Data format: integer;integer;integer
*/
34;25.3;1820918
345;56.43;18741
// This data is for customer A
835;57.34;8971
=======
How can I ignore the comment lines in my stat.txt?
Thank you.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I'd only allow one mechanism for supporting comments, and make it the easier one (ie any line starting with // is a comment, otherwise it is text)
Now when you obtain a line of text, see if s.startsWith("//") and ignore the line.
I wouldn't support multi-line comments or mixed data/comment lines at all, unless you want to write a complete text parser. (I wouldn't...)
Dave.
 
Mulyadi Kurniawan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick response! startsWith works.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
40 minutes isn't so quick, but they let us take lunch-breaks here at Javaranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!