Preferring Scanner to BufferedReader for taking input

In summary, Scanner is preferred because it manages exceptions implicitly, has a shorter declaration statement, and uses a larger buffer size.

Which one do you prefer for taking input: Scanner or BufferedReader?

  • Scanner

    Votes: 1 50.0%
  • BufferedReader

    Votes: 1 50.0%
  • Others/None of the above

    Votes: 0 0.0%

  • Total voters
    2
  • Poll closed .
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
I have seen that in many cases, teachers and authors prefer Scanner to BufferedReader for taking input from the user through keyboard.

I have found two reasons for this:

1. Scanner class manages exceptions implicitly, while for BufferedReader, exceptions have to be handled either by try..catch statements, or by the throws keyword.

2. The statement for Scanner object declaration is shorter than that for BufferedReader.

Is there any other reason why you would prefer one class to the other?

Just for statistics, I'm opening a poll to determine which is preferred the most: Scanner or BufferedReader.
 
Last edited:
Technology news on Phys.org
  • #2
Use Scanner if you want to parse input, BufferedReader if you just want raw input.
 
  • #3
The primary point is:
When languages have seemingly equal choices there is usually a reason. It is not like a favorite flavor - e.g., which one do I like.
Example in UNIX C:
poll() and select() are used to check the status of file descriptors, which in UNIX is almost everything :smile:

At first glance they look the same. So why have two when one will do? Some semantics and features are different, but primary features are very similar.
My managers insisted that we discuss this during interviews. So the concept of 'why duality?' is used to distinguish folks with good experience from those with less - at least in the managerial mind.

This is a discussion:
https://daniel.haxx.se/docs/poll-vs-select.html

I do not know of a language agnostic example of this.
 
  • Like
Likes Wrichik Basu
  • #4
Wrichik Basu said:
Is there any other reason why you would prefer one class to the other?

As @jim mcnamara points out it is not a matter of preference - for most cases, but of the needs for the case at hand.
BufferedReader reads only String objects while Scanner reads and parses text into primitive types. This implies that the latter has a more efficient mechanism for reading text and also gives access to more methods than the first.
BufferedReader uses larger buffer size than Scanner - 8x larger.
If you have a multi-threaded application, BufferedReader is synchronized while Scanner is not.
 
  • #5
QuantumQuest said:
As @jim mcnamara points out it is not a matter of preference - for most cases, but of the needs for the case at hand.
BufferedReader reads only String objects while Scanner reads and parses text into primitive types. This implies that the latter has a more efficient mechanism for reading text and also gives access to more methods than the first.
BufferedReader uses larger buffer size than Scanner - 8x larger.
If you have a multi-threaded application, BufferedReader is synchronized while Scanner is not.
These points are helpful. I was asked this question in a viva-vocé, and I didn't know the answer. Later, my teacher told I should have just mentioned the features of the two classes. I was not content with the answer, and hence wanted to know what programmers have to say.

Thanks.
 
  • Like
Likes QuantumQuest

What is the difference between Scanner and BufferedReader for taking input?

Both Scanner and BufferedReader are classes used for taking input in Java. The main difference between them is that Scanner is used for parsing and processing text-based input, while BufferedReader is used for reading characters from a stream of data. Additionally, Scanner provides additional methods for parsing data, while BufferedReader is more efficient for reading large amounts of data.

Why would one prefer Scanner over BufferedReader for taking input?

One may prefer using Scanner over BufferedReader for taking input due to its convenient methods for parsing input, such as nextInt() or nextLine(). Scanner also allows for input validation and error handling, making it a more user-friendly option.

Are there any drawbacks to using Scanner for input?

While Scanner may be more convenient for parsing input, it can also be slower and less efficient than BufferedReader for reading large amounts of data. Additionally, Scanner is not thread-safe, meaning it may cause issues in multi-threaded environments.

When should I use BufferedReader instead of Scanner?

BufferedReader is a better choice for reading large amounts of data, such as reading from a file or network stream. It is also more efficient for reading single characters. Additionally, if performance is a priority, BufferedReader may be a better option over Scanner.

Can I use both Scanner and BufferedReader for taking input in the same program?

Yes, it is possible to use both Scanner and BufferedReader for taking input in the same program. However, it is important to keep in mind their differences and use them appropriately for their intended purposes. Mixing them can result in unexpected behavior and decrease performance.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top