1. Need to connect keyboard(System.in) to input stream object we can use “InputStreamReader” that can read data from keyboard.
    InputStreamReader obj=new InputStreamReader(System.in);
    
  2. Connect InputStreamReader to BufferedReader,which is another input type of Stream.
    BufferedReader br=new BufferedReader(obj);
    
  3. Streams

  4. The above 2 steps can be combined and rewritten in a single Statement as follows.
    BufferedReader br=new 
    BufferedReader(new InputStreamReader(System.in));
    
  5. Now we can read the data from the keyboard using read() and readLine() methods are available in BufferedReader class.
Data From Keyboard Method
Reading single Character value br.read() (or)(char)br.read()
Reading String value br.readLine()
Reading int value String num=br.readLine();
int n=Integer.parseInt(num);
Reading float value String num=br.readLine();
float f=Integer.parseFloat(num);
Reading double value String num=br.readLine();
double d=Integer.parseDouble(num);
Reading byte value String num=br.readLine();
byte b=Integer.parseByte(num);
Reading short value String num=br.readLine();
short s=Integer.parseShort(num);
Reading long value String num=br.readLine();
long l=Integer.parseLong(num);
Reading boolean value String num=br.readLine();
boolean b=Integer.parseBoolean(num);

Example:Reading Employee details from the keyboard.

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Test {

	public static void main(String[] args) throws Exception {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Enter a id:");
		int id = Integer.parseInt(br.readLine());
		
		System.out.println("Enter Gender(M/F):");
		char gender = (char) br.read();
		
		System.out.println("Enter name:");
		String name = br.readLine();
		
		System.out.println("Id:" + id);
		System.out.println("Gen:" + gender);
		System.out.println("Enter name:" + name);
	}
}
/*
Output:
Enter a id:
12
Enter Gender(M/F):
M
Enter name:
Id:12
Gender:M
Name:
*/

Analysis:

  1. After accepting gender of the employee,It is not accepting name of employee and is displaying blank for name.
  2. The reason is that we used read() method to accept the gender value and then readLine() is used to accept the name.
  3. When we type M/F for gender and press enter,Then it releases a ‘\r\n’ code.So at gender column we are giving 3 characters.M.\r and \n.
  4. But,read() method takes only the first character and rejects remaining characters,i.e /r,/n ,which are trapped by next readLine() method.
        Enter gender(M/F):M \r\n
        Enter name : \r\n

    The readLine() method trapping the /r/n code.

There are 2 ways to overcome this problem.

  1. One solution is to use readLine() method
    Accepting the input for gender as a string ,i.e M \r\n using readLine() method.We are retrieving the 0th character i.e only M from it and returning it into gender variable.
  2. Clearing \r\n from BufferedReader object,So that those 2 characters are not carried into next readLine().For this purpose ,we can use skip() method of BufferedReader,which helps in skipping a specified no .of characters.Suppose we take \r\n 2 characters now to skip them We can write br.skip(2)

Modified version of above program:

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Test {

	public static void main(String[] args) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter Id:");
		int id = Integer.parseInt(br.readLine());
		System.out.println("Enter Gender(M/F):");
		char gender = (char) br.read();
		// solution1: char gender=br.readLine().charAt(0);
		// solution2:skip 2 characters
		br.skip(2);
		System.out.println("Enter name:");
		String name = br.readLine();
		System.out.println("Id:" + id);
		System.out.println("Gender:" + gender);
		System.out.println("Name:" + name);
	}
}
/*
output:
Enter Id:
234
Enter Gender(M/F):
M
Enter name:
Harold
Id:234
Gender:M
Name:Harold
*/

Note:

  1. Different Operating Systems have different ways of understanding new Line.
  2. Mac Operating System ‘\r’ is treated as newline.
  3. Unix or Linux. Operating System ‘\n’ is treated as new Line
  4. Windows Operating System needs 2 characters together to interpret as a newline(‘\r\n’).
  5. readLine() discards end of line characters.