This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StringTokenizer and End of file question  RSS feed

Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a method to read a text file, this text file has contained the user data line by line.
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())
It can read the text file. But, the output is:
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
dagmar timler
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)]
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!