Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

BufferedReader vs Scanner

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there a big difference between BufferedReader and the Scanner class? Are there advantages/disadvantages using one instead of the other?
 
Saloon Keeper
Posts: 13394
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually use BufferedReader if I need to read the entire input line by line.

If I'm looking for specific data in a file, I prefer to use Scanner, because it can search using regular expressions.

For instance, if your application reads a log or a book or something like that and displays (a part of) the contents, I would use BufferedReader.

If your application reads a configuration file and you need some named entries, I would use Scanner.
 
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect Scanner uses a buffered reader behind the scenes and it consumes any Exceptions, so Scanner is easier to use. It breaks the input into separate tokens and does the parsing for you, but there are pitfalls with nextLine.
I use Scanner by preference. As Stefan says, you usually only read a line at a time with a buffered reader.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Bishop wrote:Is there a big difference between BufferedReader and the Scanner class? Are there advantages/disadvantages using one instead of the other?


As Campbell will tell you, I'm not a big fan of Scanner.

That said: why can't you use both? One nice thing about Scanners is that you can create them for Strings; and that means that you can use a BufferedReader to read your "lines" (or keyboard input until your user presses <ENTER>), and then use a Scanner to "break up" (or validate) the "lines" you receive.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is something about Scanners which people often forget. You often use Scanner#hasNextInt for keyboard input, but you can also use it for Strings from any source.There is a bit more about "\\s*,\\s*" here.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic