Preferring Scanner to BufferedReader for taking input

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

Discussion Overview

The discussion revolves around the preference between the Scanner and BufferedReader classes in Java for taking input from users. It explores the reasons for choosing one over the other, including aspects of functionality, efficiency, and specific use cases.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • Some participants note that Scanner manages exceptions implicitly, while BufferedReader requires explicit exception handling.
  • It is mentioned that the declaration statement for Scanner is shorter than that for BufferedReader.
  • One participant suggests using Scanner for parsing input and BufferedReader for raw input.
  • Another participant discusses the concept of duality in programming languages, using UNIX C as an example, and questions why two similar options exist.
  • Some participants highlight that BufferedReader reads only String objects, whereas Scanner can read and parse text into primitive types, suggesting that Scanner has a more efficient mechanism for reading text.
  • It is pointed out that BufferedReader has a larger buffer size compared to Scanner and is synchronized, making it potentially more suitable for multi-threaded applications.

Areas of Agreement / Disagreement

Participants express differing views on the preference for Scanner versus BufferedReader, indicating that the choice often depends on the specific needs of the application rather than a clear consensus on one being superior to the other.

Contextual Notes

Some limitations in the discussion include the lack of consensus on when to use each class and the dependence on specific use cases, which may not be fully explored in the conversation.

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 .
Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,186
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:
Technology 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

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
21
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K