Scanner vs BufferedReader?
I was just curious as to why I see many people in this forum using BufferedReader over the Scanner class? I don't believe I've heard of it before I came here, I've just always used the Scanner class.
Is there any particular reason why someone would choose BufferedReader? I've not run into any problems or confusing using Scanner, but if there is a better advantage of BufferedReader, I may start using it.
Re: Scanner vs BufferedReader?
I like using Scanner... Scanner allows you to parse the input in certain ways (ex. next(), nextInt(), nextDouble(), etc.), while BufferedReader is designed to handle streams without altering the content.
Re: Scanner vs BufferedReader?
Quote:
Originally Posted by
helloworld922
I like using Scanner... Scanner allows you to parse the input in certain ways (ex. next(), nextInt(), nextDouble(), etc.), while BufferedReader is designed to handle streams without altering the content.
Thanks for the comment. I guess I'll keep using Scanner then. Probably easier than learning a whole new class anyway to accomplish the same tasks with a class I already know.
Re: Scanner vs BufferedReader?
i hope youll take this one.. hehe ... its far more easy to declare....
no parsing...
Re: Scanner vs BufferedReader?
Other than what helloworld said, Scanner internally buffers its input using a CharBuffer. Whereas a BufferedReader is meant for generic use for buffering characters from some sort of input.
Re: Scanner vs BufferedReader?
So, Scanner is better in the sense that it allows more specific operations to be done. And BufferedReader is more of a generic catch all, which seems to me more like a class that would be taught in Computer Science classes for beginners since it seems to be more "simple" to use. I was always taught Scanner so it just comes naturally to me, but from what I can gather, Scanner is more powerful?
Re: Scanner vs BufferedReader?
It really is up to the professor. When I took CS 1 at college, my professor taught us Scanner. Yet, two semesters later a new professor taught the course with BufferedReader. (However, I believe he introduced Scanner later in the course.)
In a sense Scanner is more powerful. It deals with parsing the data so you don't have to. The rule of thumb is if you want more than just character data, then use a Scanner. If all you want is character data (like if you were writing a compiler, or reading from a network stream) then a BufferedReader is all you need. A Scanner would be overkill.
Re: Scanner vs BufferedReader?
Scanner is easier to use for general stuff. However, when you want to parse a stream of input rather than a file with discrete values, BufferedReader is much better because you can decide what it means to read in the next item, where as Scanner defines next as if the input stream will be a String (specifically, words separated by white spaces).
Re: Scanner vs BufferedReader?
Ok, that makes sense. So unless I'm doing something with reading files, I can use BufferedReader to accept the input, as long as that input is generated by the user, such as entering numbers or text, and not a file, right?
I really appreciate all of your input! Thanks!
Re: Scanner vs BufferedReader?
It really doesn't matter where the input comes from. For example:
Scanner in = new Scanner(new File("myfile.txt");
BufferedReader in = new BufferedReader(new FileReader(new File("myfile.txt"));
It all depends on what you need to do.
Re: Scanner vs BufferedReader?
The Scanner class is a simple text parser which can parse primitives as well as strings using regular expression.
However a Reader is character stream reader which just reads each character after another.
BufferedReader is a convenience class extending Reader which provides you with a nice readLine method. This method reads the character stream and stops at a line break and then returns whatever it has stored.
// Json
Re: Scanner vs BufferedReader?
Ok, that all makes sense now. Thanks a lot to everyone!