Scanner or BufferedReader ?
This is not much of a question, but a discussion and searching for opinions. I have seen that many use the Scanner class for getting input from the keyboard. Some, like myself, use the BufferedReader.
Why will someone use one over the other?
Or they both do the same thing when it comes something simple as that and their real difference is when it comes to using them for something more complex?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Well im voting for the scanner. Thats how i learnt and thats how i go, i dont find anything wrong with it so i stay that way.. addmitedly i really dont know a thing about Buffered Reader anyway, so it could be loads better for all i know.
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
Neither one. I don't write console applications. :P
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Ehmmm where do I find this in Java Microedition? :?:
Neither me. For now I work in JME, Swing or Web Development so no.
peter_budo
Code tags enforcer
15,432 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
Neither one. I don't write console applications. :P
I don't write console applications either but when I first begun I knew from my book only the BufferedReader. So since we've seen many students here asking how to read files and input from the console I thought to get other opinions and discuss the topic.
There has to be some reason why someone would choose to use one over the other
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
I don't write console applications either but when I first begun I knew from my book only the BufferedReader.
That was most probably because when we learnt Java, java.util.Scanner did not exist.
I started learning Java from the 1.4.2 version of the JDK, and Scanner was introduced only in the 1.5 version and since console input is not so widely used by many of us, we opted to stick with good ol' BufferedReader.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
That was most probably because when we learnt Java, java.util.Scanner did not exist.
I started learning Java from the 1.4.2 version of the JDK, and Scanner was introduced only in the 1.5 version and since console input is not so widely used by many of us, we opted to stick with good ol' BufferedReader.
Well the book I had was called Java 1.2. I don't remember what kind of java I had because I didn't know about versions but I believe I had Java Version 1.2
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Well the book I had was called Java 1.2. I don't remember what kind of java I had because I didn't know about versions but I believe I had Java Version 1.2
Exactly that explains the reason why like myself you too have opted for the BufferedReader, Scanner just never existed during our initial console programming days in Java.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
IMO Scanner is much more than just another I/O class with a pretty interface/API; it has advanced parsing capabilities which attempts to remove the utility class most programmers create to process the strings read in by a normal reader. It is pretty much capable of reading and performing complicated processing on strings irrespective of the source [Strings, InputStreams, Readers]. The feature of skipping/locating patterns based on regular expressions is powerful in itself.
On the other hand, the API specifies that the BufferedReader class *should* provide buffering capabilities, something not mandated when implementing a Scanner .
To conclude it all, I still end up using BufferedReader and my custom string processor when reading and processing textual data. Nothing against Scanner mind you, just historical reasons. :-)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
> 2. If reading in from a file, bufferedreader, because it's faster.
That's a non-issue unless you are reading a lot of pretty big files. For reading one-off files in an application (which are around a few KiB's), I go with Scanner just because it's less typing.
An implementation detail which the users of Scanner class need to keep in mind is that it is memory/cpu heavy (at least when compared to BufferedReader) because it internally uses "regular expressions" for matching your "nextXXX" as opposed to just reading everything till the end of line as in the case of a regular Reader.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733