Input by BufferedReader

Input by BufferedReader

BufferedReader is a input type of stream.

To accept data from keyboard, i.e. System.in, we need  to connect it to an input stream as some input stream is needed to read data.

Connect the keyboard to an input stream object. Here we can use InputStreamReader that can read data from the keyboard.

InputStreamReader obj = new InputStreamReader(System.in);

Connect InputStreamReader to BufferedReader, which is another input type of stream. We are using BufferedReader as it has got method to read data properly, coming from the stream.

BufferedReader br = new BufferedReader(obj);

These two steps can be combined and rewritten in a single statement as

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Example
import java.io.*;
class test
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter Employee ID”);
int id=Integer.parseInt(br.readLine());
System.out.println(“Enter Employee Name”);
String name=br.readLine();
System.out.println(“Employee ID is “+id);
System.out.println(“Employee Name is “+name);
}
}

Output:
Enter Employee ID
1
Enter Employee Name
Mr D
Employee ID is 1
Employee Name is Mr D

Leave a Reply

Your email address will not be published. Required fields are marked *