Preferring Scanner to BufferedReader for taking input

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
Insights Author
Gold Member
2020 Award
1,655
1,530
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:

Answers and Replies

  • #2
1,524
624
Use Scanner if you want to parse input, BufferedReader if you just want raw input.
 
  • #3
jim mcnamara
Mentor
4,227
2,792
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
QuantumQuest
Science Advisor
Insights Author
Gold Member
926
485
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
Wrichik Basu
Insights Author
Gold Member
2020 Award
1,655
1,530
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

Related Threads on Preferring Scanner to BufferedReader for taking input

  • Last Post
Replies
2
Views
10K
Replies
1
Views
9K
  • Last Post
Replies
11
Views
2K
  • Last Post
Replies
16
Views
3K
Replies
5
Views
971
Replies
2
Views
3K
  • Last Post
Replies
18
Views
1K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
0
Views
2K
Top