*
The moose likes Beginning Java and the fly likes BufferedReader & StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BufferedReader & StringTokenizer" Watch "BufferedReader & StringTokenizer" New topic
Author

BufferedReader & StringTokenizer

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I've read in lines from a text file before without any difficulties. However the text file i'm reading now has alot of semicolons, periods etc..
So I use a delimiter with alot of parameters. The problem is that the following code doesn't read in the entire text file, it starts about 200 lines down and goes right to the end. Even if I get rid of the delimiter, it doesn't start at the beginning of the file...Can anyone help me out with my code?

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok,
well i think it will be better if you use the "split method "of String class instead of using the String Tokenizer.Below shown is the link where you will find the description of split method and the way you can use it.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

I hope if it helps you.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
John,

If I've understood your description correctly (at this late hour), I think your program does just what you were expecting it to do, provided a few additional pieces are included.

When posting the example code, I'm guessing you maybe left out the pieces where you print or otherwise use the tokens read in.

For the following code example, I copied and pasted your code, and then I added a couple of lines that print the input data (in "raw" and "tokenized" fashions). I think it does what you want it to do. (Below, I'll also paste the "input.txt" file I tested with.)

To otherwise guess at what ailment is plaguing you... Do you perhaps have a development environment problem, where maybe an old version of your program code is mistakenly being run, or where maybe an old version of the input data is mistakenly being used?

Foo.java


input.txt


output


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I think the code works too. I think the problem is, is that my text file being read contains ALOT of lines and words. After further testing of my program I see that it is indeed processing all the lines and words properly. However, I still don't understand why they don't print to screen properly when I try and print the tokens or lines. It always starts at line, let's say 300 and prints out the results. When indeed it is processing previous lines. Very strange! Thanks for the help though!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't see anywhere in the code you originally posted where you write anything out anywhere. However let's suppose you actually have some code like what Dirk posted, and you're just writing the tokens out to the console.

Then, if you can't see the beginning of your output, it probably just scrolled off the top of the console window.
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Nope, scrolled to the top of the console window and found it started at a later line. That's why I assumed it wasn't starting from the beginning...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The console window probably threw away the first 300 lines.

But let's not waste time speculating, especially since I have no idea what kind of console you're using. Write your output to a file instead and see what happens.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BufferedReader & StringTokenizer
 
Similar Threads
How can I create an object based on input
Reading Java File
Manipulating Vectors (code not working)
Need some help!!!
Reading and Writing txt files