Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer and End of file question

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wrote a method to read a text file, this text file has contained the user data line by line.
e.g.
Tom, 34, M
Mary, 25, F
David, 27, M

BufferedReader in = new BufferedReader(new FileReader(VarConst.FILENAME));
String s = "";
while ((s = in.readLine()) != null )
{
StringTokenizer st = new StringTokenizer(s, VarConst.SEPARATOR);
while (st.hasMoreTokens())
{
System.out.print(st.nextToken());
}
}
It can read the text file. But, the output is:
Tom34MMary25FDavid27M
How can I display them line by line like those in the text file?
Secondly, how can I count the total number of line/record in the text file?
I cannot use length() to get it.
How can I detect the end of the file or last record in the text file?
Thanks for help
Andrew
 
dagmar timler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The answer to your second question is easy - how do you find out the number of lines? Simply add a counter variable in the "while ((s=in.readLine()) != null)" loop and whatever the value of the variable when you have finished the loop is the number of lines.
The answer to your first question is trivial. You could simply add the VarConst.SEPARATOR after every System.out and before the end of your "while ((s=in.readLine()) != null)" loop, add a System.out.println("\n"). You could also create a StringTokenizer that returned the delimeter with the tokens it extracts [public StringTokenizer(String str, String delim, boolean returnDelims)]
Cheers,
Dagmar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic