• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nature of a BufferedReader ?

 
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
Marshal
Pie
Posts: 24211
35
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: 2985
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