Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Efficiency! Design the UI

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Difference between Scanner and BufferedReader

Hey folks,

can someone explain the difference ...

1 Answer

Enrique Munguía
Enrique Munguía
14,311 Points

Scanner is a class specialized in parsing primitives and strings from a text source, this source can be a text file, the input from the keyboard and so on, it provides methods to detect and find those types among other things. When you start programming in Java you are advise to use Scanner to read user input from the keyboard because of its simplicity.

BufferedReader is a general purpose class for read text from a character-input stream this can be a text file or input from the keyboard too, but as the name implies, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. If the text you are reading contains numbers or other primitives it is up to you to manually parse those primitives to use them in your program.

When to use one or another, my rule of thumb is that if you want to parse primitives from short amount of text you use Scanner, when you want to read large amounts of text (and optionally parse some sections of it) you use BufferedReader.