|
![]() |
On the debate of which of the following:
while( ( line = myFile.readLine() ) != null )
boolean done = false ;
while ( ! done ) ... if <something> { done = true } else ....
I tend to fall in the camp of preferring the first. But of course those of us doing the Cattle Drive assignments have to comply with the style guide.
Aside from the Cattle Drive assignments, however, this entire debate suggests to me that the public methods of BufferedReader (or whatever object you're calling readLine() on) aren't as useful as they could be. As a client of some class, I think having to check for != null on something its method returns is so loseresque. I mean, really, everyone knows we want to cycle through the lines of the file and treat each line as a String. So why wasn't BufferedReader (or suchlike) written like that to begin with? Hey, it's starting to sound like instead of a loser method like readLine, I'd prefer working with an iterator or something iteratoresque.
A sample wrapper class that returns an iterator follows:
Would allow clients to write tidy code like this:
A sample wrapper class that provides iteratoresque methods follows. Note that in this case we get to deal with Strings as Strings with no pesky downcasting.
Clients could write:
Note that in neither case does client code have to do any pesky checking for not null.
Discuss!
Just realized that an Iterator's next method is supposed to throw NoSuchElementException if there's no next (instead of returning null like mine does). Anyhow, you get the picture.
I do really like Michael's idea about using an iterator of some sort - this would be nicer for everyone I think. Too bad they didn't put this in the language to begin with. But Michael's code is a step in the right direction. In its current form it is utterly evil of course, due to the catch block which ignores an IOExceptionBut this can be easily excorcised. I'd probably decouple the new class from reading a file - make it a more generic FilterReader. In fact it could extend BufferedReader. Give it a method lineIterator() which returns a LineIterator - which might as well be an actual Iterator with one additional method, nextLine(), which returns a String rather than Object to avoid a needless cast. Usage would be like
Or, in more Coop-friendly style:
The LineReader class could also include the functionality of the seldom-used LineNumberReader class, just cause it's so easy.
It would be really nice if Java had a nice foreach keyword that would allow something like this:
But I guess I'm dreaming now. I might as well switch to Perl. Or for that matter, C#. :roll:![]()
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
[b]So while it might not be particularly necessary to replace the trusty sentinel check with a new idiom, it's at least a fun excercise in design.
-Originally posted by Michael Matola:
Maulin and Barry have very validly pointed out what's wrong with the hasNext method in my code.
Interestingly, I think the easiest way to make hasNext atomic -- detect the state but not change it --, is to load the whole file into some data structure (array, list, etc.) (all at once would probably be easier than incrementally). Which pretty much gets us to Jim's revised readLines
And as far as Barry's nitpick about if ( hasNext ) instead of if ( hasNext == true ), all I can say is that Barry knows I knows this because he's seen code of mine in which I *do* get it right.![]()
"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
"I'm not back." - Bill Harding, Twister
Consider Paul's rocket mass heater. |