Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BufferedReader problem

 
brian yuen
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try BufferedReader.mark() before starting the if block, and then use BufferedReader.reset() before starting the while block.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Michael
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
brian yuen
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys..i got it now
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q:
-----------------------------------
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic