Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Scanner vs BufferedReader?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Scanner vs BufferedReader?

    I was just curious as to why I see many people in this forum using BufferedReader over the Scanner class? I don't believe I've heard of it before I came here, I've just always used the Scanner class.

    Is there any particular reason why someone would choose BufferedReader? I've not run into any problems or confusing using Scanner, but if there is a better advantage of BufferedReader, I may start using it.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Scanner vs BufferedReader?

    I like using Scanner... Scanner allows you to parse the input in certain ways (ex. next(), nextInt(), nextDouble(), etc.), while BufferedReader is designed to handle streams without altering the content.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Bill_H (October 26th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Scanner vs BufferedReader?

    Quote Originally Posted by helloworld922 View Post
    I like using Scanner... Scanner allows you to parse the input in certain ways (ex. next(), nextInt(), nextDouble(), etc.), while BufferedReader is designed to handle streams without altering the content.
    Thanks for the comment. I guess I'll keep using Scanner then. Probably easier than learning a whole new class anyway to accomplish the same tasks with a class I already know.

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Talking Re: Scanner vs BufferedReader?

    i hope youll take this one.. hehe ... its far more easy to declare....
    no parsing...

  6. #5
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Scanner vs BufferedReader?

    Other than what helloworld said, Scanner internally buffers its input using a CharBuffer. Whereas a BufferedReader is meant for generic use for buffering characters from some sort of input.

  7. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Scanner vs BufferedReader?

    So, Scanner is better in the sense that it allows more specific operations to be done. And BufferedReader is more of a generic catch all, which seems to me more like a class that would be taught in Computer Science classes for beginners since it seems to be more "simple" to use. I was always taught Scanner so it just comes naturally to me, but from what I can gather, Scanner is more powerful?

  8. #7
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Scanner vs BufferedReader?

    It really is up to the professor. When I took CS 1 at college, my professor taught us Scanner. Yet, two semesters later a new professor taught the course with BufferedReader. (However, I believe he introduced Scanner later in the course.)

    In a sense Scanner is more powerful. It deals with parsing the data so you don't have to. The rule of thumb is if you want more than just character data, then use a Scanner. If all you want is character data (like if you were writing a compiler, or reading from a network stream) then a BufferedReader is all you need. A Scanner would be overkill.

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Scanner vs BufferedReader?

    Scanner is easier to use for general stuff. However, when you want to parse a stream of input rather than a file with discrete values, BufferedReader is much better because you can decide what it means to read in the next item, where as Scanner defines next as if the input stream will be a String (specifically, words separated by white spaces).

  10. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Scanner vs BufferedReader?

    Ok, that makes sense. So unless I'm doing something with reading files, I can use BufferedReader to accept the input, as long as that input is generated by the user, such as entering numbers or text, and not a file, right?

    I really appreciate all of your input! Thanks!

  11. #10
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Scanner vs BufferedReader?

    It really doesn't matter where the input comes from. For example:

    Scanner in = new Scanner(new File("myfile.txt");

    BufferedReader in = new BufferedReader(new FileReader(new File("myfile.txt"));


    It all depends on what you need to do.

  12. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Scanner vs BufferedReader?

    The Scanner class is a simple text parser which can parse primitives as well as strings using regular expression.

    However a Reader is character stream reader which just reads each character after another.

    BufferedReader is a convenience class extending Reader which provides you with a nice readLine method. This method reads the character stream and stops at a line break and then returns whatever it has stored.

    // Json

  13. #12
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Scanner vs BufferedReader?

    Ok, that all makes sense now. Thanks a lot to everyone!

Similar Threads

  1. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM
  2. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM
  3. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 17th, 2009, 11:07 PM
  4. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 06:55 PM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM