wood burning stoves 2.0
The moose likes Beginning Java and the fly likes BufferedReader problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BufferedReader problem" Watch "BufferedReader problem" New topic

BufferedReader problem

brian yuen

Joined: Oct 20, 2003
Posts: 20

if the text file is like
1212121212,yuen,chi king,412304265,hotmail.com,1212121212// if loop begin
1111122222,chow,wing yee,403546264,cti,1111122222 //while loop begin
how can i make the while loop read from the beginning again, thanks for help, i know that may be a very easy question..
[ October 23, 2003: Message edited by: brian yuen ]
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Try BufferedReader.mark() before starting the if block, and then use BufferedReader.reset() before starting the while block.

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Brian
Your problem here is that you want to start tokenizing the 'line' variable in your while loop, but you have already reassigned it by the time that loop begins. If you take the assigment expression out of the conditional expression in your while loop, this should give you what you want. So, where you currently have:

change it to read

You will then need to re-assign the 'line' variable as the last line of your while loop:

Hope this helps

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This might be saying the same thing ...

This "primes the pump" before the loop, at the cost of duplicating the read call.
Or you might use your original loop but detect error differently. I think I like this one better:

In fact we could put that into a method that returns linecount instead of doing the error:

You might guess I get uncomfortable with methods that seem to do two things. "check for error and process lines if no error" is a slightly complicated description for a method, a hint to break it up a bit. Hope that makes sense.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
brian yuen

Joined: Oct 20, 2003
Posts: 20
Thanks guys..i got it now
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
while((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, "," ); String getCusId = st.nextToken(); System.out.println(getCusId); if (getCusId.equals(customerId)) { test = true; } }

If the line is null program will exit by calling EXIT() method.
So it comes to while loop if line is not null only. In this case it will never fail and checking again for null could be removed.
So, I think you can use the simple logic of do while loop.
do { StringTokenizer st = new StringTokenizer(line, "," ); String getCusId = st.nextToken(); System.out.println(getCusId); if (getCusId.equals(customerId)) { test = true; } }while((line = br.readLine()) != null)
Is that OK?

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: BufferedReader problem
It's not a secret anymore!