Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can same BufferedReader take multiple inputs?

 
Yudi Strange
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using the same buffer reader for taking different inputs. But it's not working properly. Please explain why?


 
anandhi mohan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use same buffer reader to get different input.
In your code, you are closing the bufferreader before it reads the input thats why it throws some exceptions.

try this:

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
String a;
System.out.println("Enter Name");
a=br.readLine();
nme=a;
System.out.println("Enter Amount");
a=br.readLine();
bn=Double.parseDouble(a);
System.out.println("Enter rollno.");
a=br.readLine();
double x=Double.parseDouble(a);
Long ac_no=(long)x;
br.close();
}
catch(Exception e)
{

System.out.println("Exception encountered");
}
 
Lei Guoguo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

uper code is java source in java.io.BufferedReader, you know , in this method, in = null;

see under code

key code is super(in);
now , we can see the super class java.io.Reader


i think, you are understood, the inputstream in bufferedreader is null when we close the invoke java.io.BufferedReader.close()
 
Yudi Strange
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic