This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

nature of a BufferedReader ?  RSS feed

 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer tokens = new StringTokenizer(stringToTokenize);
while(tokens.hasMoreTokens()) {
String s = tokens.nextToken();
while ((wordToExclud = in.readLine()) != null) {
if(s.equalsIgnoreCase(wordToExclud))
processedString += s + " ";
}
}
in.close();

If "in" is a bufferedReader, containing 100 lines of a text file, what happens within the innner while loop after line 100 ?

Will "in" go to line 1, on the outter whiles second iteration ?

eghagha
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You get to the end, and then readLine() returns null. It never "rewinds".
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could overcome your dilemma by reading all the lines into a data structure, and then using that to compare to you string tokens. Think very carefully about the amount of work you are doing though. If there are 50 tokens and 100 lines, then you will be doing 5000 string comparisons. You could reduce that number by using a binary search, or at least by breaking out of the inner loop once a match was found.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!