File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

StringTokenizer and End of file question

Andrew Parker
Ranch Hand

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

Joined: Nov 16, 2001
Posts: 11
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer and End of file question