• 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

More than one BufferedReader

 
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic