File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes StringTokenizer and End of file question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringTokenizer and End of file question" Watch "StringTokenizer and End of file question" New topic

StringTokenizer and End of file question

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
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

Joined: Nov 16, 2001
Posts: 11
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)]
I agree. Here's the link:
subject: StringTokenizer and End of file question
jQuery in Action, 3rd edition