• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner vs. BufferedReader/ InputStreamReader

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My instructor informed the class on Friday that for future assignments, we were not to use the Scanner class for getting data from standard input as she considers it a "beginner's" class. She wants us to use an InputStreamReader wrapped in a BufferedReader because this gives us "more control". My question is, is she right? I seems to me that Scanner class used correctly can be pretty powerful with its support for regex's, and ability to parse numerals "on the fly". I know that earlier versions of java are not compatible with the Scanner class but this was not the reason she gave for forbidding its use. Just interested in any feedback or opinion.

Garrett
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe she just wants the class to get used to the idea of using streams, since they are used for alot of things in java. I think maybe by beginner she just meant that its concealing the "tuff" part of doing I/O. Just my opinion
-Luc
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by luc comeau:
Maybe she just wants the class to get used to the idea of using streams, since they are used for alot of things in java. I think maybe by beginner she just meant that its concealing the "tuff" part of doing I/O. Just my opinion
-Luc



Thats a possibility, but that doesn't explain her assertion (in my mind) that the BufferedReader offers "more control" over the input stream. But since she is just beginning to cover exception handling, this is a definite possibility.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't use Scanner very much, I still rely on BufferedReader a lot. Looking at the documentation for Scanner, it seems that a central theme of many of its methods is the idea of parsing the input stream into tokens. BufferedReader doesn't rely on breaking up its input into tokens. It allows you to read character by character if you want. It can read an entire line and let you do what you want. You can also read chunks of character arrays using the BufferedReader. So I would say, just as a personal opinion, that you do have more flexibility with BufferedReader.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
\BufferedReader doesn't rely on breaking up its input into tokens. It allows you to read character by character if you want. ...You can also read chunks of character arrays using the BufferedReader. So I would say, just as a personal opinion, that you do have more flexibility with BufferedReader.



I can live with that. Scanner has no facility for reading character by character except reading an entire line and breaking it up into a character array.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Keith]: BufferedReader doesn't rely on breaking up its input into tokens. It allows you to read character by character if you want. It can read an entire line and let you do what you want.

You can easily read a whole line with Scanner too, using nextLine(). It's also possible to read one character at a time with next() if you useDelimiter(""), but this is not obvious. Of course you can also read a line or other String and then use charAt() to look it individual chars if that's what you want.

The one thing I don't quite like about Scanner is that if an IOException occurs in an underlying stream, it can be easily overlooked. Admittedly the most common IOExceptions are probably FileNotFound, which is thrown as expected by the new Scanner(File) constructor if there's a problem finding a file. But if other problems occur after the Scanner is created - you lose a network connection, for example - then it looks like the various hasNextXXX() methods will simply return false. You really should check the value of scanner.ioException() when you're done, so see if everything's really OK:

This isn't any more work than catching exceptions from stream - the danger though is that Scanner makes it all too easy to simply forget to do it. Something to beware of...
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic