File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Scanner vs. BufferedReader/ InputStreamReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner vs. BufferedReader/ InputStreamReader" Watch "Scanner vs. BufferedReader/ InputStreamReader" New topic
Author

Scanner vs. BufferedReader/ InputStreamReader

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
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


National Research Council<br />Internet Logic Department
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 17, 2006
Posts: 1296
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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...


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner vs. BufferedReader/ InputStreamReader
 
Similar Threads
About printing and reading
i/o question
problems with .next() .nextLine(); Loop
Writing a class file to read text file.
cant seem to end while loop