943,865 Members | Top Members by Rank

View Poll Results: Scanner or BufferedReader?
Scanner 3 33.33%
BufferedReader 6 66.67%
Voters: 9. You may not vote on this poll

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 13356
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 22nd, 2009
0

Scanner or BufferedReader ?

Expand Post »
This is not much of a question, but a discussion and searching for opinions. I have seen that many use the Scanner class for getting input from the keyboard. Some, like myself, use the BufferedReader.

Why will someone use one over the other?

Or they both do the same thing when it comes something simple as that and their real difference is when it comes to using them for something more complex?
Similar Threads
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Jan 22nd, 2009
0

Re: Scanner or BufferedReader ?

Well im voting for the scanner. Thats how i learnt and thats how i go, i dont find anything wrong with it so i stay that way.. addmitedly i really dont know a thing about Buffered Reader anyway, so it could be loads better for all i know.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Jan 22nd, 2009
0

Re: Scanner or BufferedReader ?

Well im voting for the scanner. Thats how i learnt and thats how i go, i dont find anything wrong with it so i stay that way.. addmitedly i really dont know a thing about Buffered Reader anyway, so it could be loads better for all i know.
Ditto... If i ever begin to study BufferedReader I'll discuss it further. What are the differences anyway? Any one that is advanced plz reply?
Reputation Points: 10
Solved Threads: 4
Light Poster
jrdark13 is offline Offline
38 posts
since Jan 2009
Jan 22nd, 2009
0

Re: Scanner or BufferedReader ?

Neither one. I don't write console applications.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jan 22nd, 2009
0

Re: Scanner or BufferedReader ?

Ehmmm where do I find this in Java Microedition?

Neither me. For now I work in JME, Swing or Web Development so no.
Last edited by peter_budo; Jan 22nd, 2009 at 3:57 pm.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jan 22nd, 2009
0

Re: Scanner or BufferedReader ?

I used to use the buffered reader in September but I started to find when in my contests that the Scanner is better.

Since I can read individual elements at a time, I don't have to worry about splitting and parsing into integers.

Say my input file is:

Quote ...
4 4
*...
....
.*..
With the buffered reader, I would have to read things in line at a time, split it up and parse it into integers. Thus the buffered reader seems to make input a lot tougher.

Buffered reader:

Java Syntax (Toggle Plain Text)
  1. BufferedReader br = new BufferedReader(new FileReader("DATA1.txt"));
  2. String[] arsNums = br.readLine().split(" ");
  3. int nX = Integer.parseInt(arsNums[0]);
  4. int nY = Integer.parseInt(arsNums[1]);

With the scanner I don't have to read in each line as a string and parse it with the Integer class.

Scanner:

Java Syntax (Toggle Plain Text)
  1. Scanner sin = new Scanner(new FileReader("DATA1.txt"));
  2. int nX = sin.nextInt();
  3. int nY = sin.nextInt();
  4. char[][] arcGrid = new char[nX][nY];
  5. String sLine = "";
  6.  
  7. for (int x=0;x<nX;x++) {
  8. sLine = sin.next();
  9. for (int y=0;y<nY;y++) {
  10. arcGrid[x][y] = sLine.charAt(y);
  11. }
  12. }

Also I can have Scanner that parses each individual element in a String. So the Scanner class is quite helpful - and in my opinion a lot better than the buffered reader.

Java Syntax (Toggle Plain Text)
  1. String sText = "Four men went down to the store, to catch frogs!";
  2. Scanner sin = new Scanner(sText);
  3. while (sin.hasNext()) {
  4. System.out.println(sin.next());
  5. }

Prints:

Quote ...
Four
men
went
down
to the
store,
to
catch
frogs!
So that is why I believe the Scanner class is better than the Buffered Reader.
Reputation Points: 12
Solved Threads: 2
Newbie Poster
chili5 is offline Offline
21 posts
since Dec 2008
Jan 23rd, 2009
0

Re: Scanner or BufferedReader ?

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Neither one. I don't write console applications.
I don't write console applications either but when I first begun I knew from my book only the BufferedReader. So since we've seen many students here asking how to read files and input from the console I thought to get other opinions and discuss the topic.

There has to be some reason why someone would choose to use one over the other
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Jan 23rd, 2009
0

Re: Scanner or BufferedReader ?

I guess it's just the matter of what you like, and on which is a lot easier. I have learn buffered reader. My prof. didn't teach us scanner. I heard about it from others that it is much easier to use, but i don't know. My prof. doesn't allow us to use scanner. I don't know why.
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Jan 23rd, 2009
0

Re: Scanner or BufferedReader ?

Click to Expand / Collapse  Quote originally posted by javaAddict ...
I don't write console applications either but when I first begun I knew from my book only the BufferedReader.

That was most probably because when we learnt Java, java.util.Scanner did not exist.
I started learning Java from the 1.4.2 version of the JDK, and Scanner was introduced only in the 1.5 version and since console input is not so widely used by many of us, we opted to stick with good ol' BufferedReader.
Last edited by stephen84s; Jan 23rd, 2009 at 7:13 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Jan 23rd, 2009
0

Re: Scanner or BufferedReader ?

Click to Expand / Collapse  Quote originally posted by stephen84s ...
That was most probably because when we learnt Java, java.util.Scanner did not exist.
I started learning Java from the 1.4.2 version of the JDK, and Scanner was introduced only in the 1.5 version and since console input is not so widely used by many of us, we opted to stick with good ol' BufferedReader.
Well the book I had was called Java 1.2. I don't remember what kind of java I had because I didn't know about versions but I believe I had Java Version 1.2
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Cant find out why exception is being thrown
Next Thread in Java Forum Timeline: java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC