Java Preferring Scanner to BufferedReader for taking input

Click For Summary
Teachers and authors often prefer the Scanner class over BufferedReader for user input due to its implicit exception management and shorter declaration syntax. Scanner is more efficient for parsing input into primitive types and offers a wider range of methods, while BufferedReader is better suited for reading raw strings and has a larger buffer size, making it more efficient for bulk reading. BufferedReader is also synchronized, which is advantageous in multi-threaded applications, whereas Scanner is not. The choice between the two classes typically depends on the specific needs of the application rather than personal preference. Understanding the reasons behind the duality of these classes can help distinguish experienced programmers from novices.

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,180
Reaction score
2,721
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 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 QuantumQuest
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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