Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

More than one BufferedReader  RSS feed

 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt about bufferedreader:

Can a single buffered object can read more than one console input ?

Suppose,



Thanks,
Ray
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

But don't use read(). Horrible method which reads one keystroke and widens it to an int. The readLine method is better.
I would suggest you stop using a buffered reader for keyboard input and use a Scanner instead. But beware of its nextLine method.
 
Tony Docherty
Bartender
Posts: 3205
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add some additional information, the reason the second read doesn't appear to work is because read() just reads the next character in the stream. So, if you enter a name with more than 1 letter eg Henry, the first call to read gets the first letter 'H'. When you call read again it doesn't wait for you to enter a roll number because there is still 'enry\n' in the input stream so it gets the 'e' and continues. If you enter a single letter name it still doesn't work because pressing the Enter key adds a '\n' char to the stream so the second read gets that. As Campbell has said use readline() or better still use a java.util.Scanner.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!