Preferring Scanner to BufferedReader for taking input

  • Context: Java 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Input Java Scanner

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 .
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
Messages
2,188
Reaction score
2,694
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:
Physics news on Phys.org
Use Scanner if you want to parse input, BufferedReader if you just want raw input.
 
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   Reactions: Wrichik Basu
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.
 
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   Reactions: QuantumQuest