There are many ways to read data from the keyboard in java:
1.       InputStreamReader Class
2.       Scanner Class
3.       Using Command Line Arguments
4.       Console Class  
 1.  InputStreamReader Class:

InputStreamReader class can be used to read data from keyboard for two reasons:

 I.     Connect to input stream of keyboard

  II.    It’s helpful for converting byte-oriented stream to character-oriented stream. 

BufferedReader class can be used to read data line by line using its readLine () method.

For example, we have want to take an input from the keyboard using InputStreamReader class.


public class InputByBufferedReader

public static void main(String[] args)throws IOException

InputStreamReader isr= new InputStreamReader(;

BufferedReader br= new BufferedReader(isr);

System.out.println("Enter your nic name: ");

String name = br.readLine();

System.out.println("Enter your Address : ");

String add = br.readLine();

System.out.println("your Nic Name : "+name);

System.out.println("your Address is : "+add);



    Enter your nic name: xxx

    Enter your Address : YYY

     your nic name: xxx

      your Address is :YYY  

2.  Scanner Class:

The java.util.Scanner class breaks the input into tokens using delimiter (i.e. Whitespace (By Default)). Its provide various methods to read and parse the various primitive values.
Scanner class is used to parse text for string and primitive types using regular expressions.
For example, we have taken a string. Java Scanner class which reads the int, string value as an input from that string.

import java.util.Scanner;
public class InputByScannerDelimeter
public static void main(String[] args)
{String Query= "Hi I am only 24";
// The \s represents whitespace.
Scanner sc= new Scanner(Query).useDelimiter("\\s");
}catch(Exception e){System.out.println(e);}






3.  Using Command Line Arguments:

 In java, Command Line Arguments is a one of the way to passed arguments at the time of running the java program.The arguments can be passed from the console can be received in the java program and it can be used as input. We have pass N numbers of arguments from the command prompt.

 For example, We have taken input from the console at the time of running the java program and sum of these integer input. 

public class InputUsingCMD{
public static void main(String[] args){
int sum=0;
for(int i=0;i<args.length;i++){
System.out.println("The sum of the arguments is "+sum);}}


Java InputUsingCMD 1 2 3 5

The sum of the arguments is 11


4.  Console Class

The java console class is be used to get input from console. It provides methods to read text and password.

If you read password using Console class, it will not be displayed to the user.

The class is attached with system console internally.

For example ,To read text from console.

class InputByConsole
public static void main(String args[]){ 
Console con=System.console(); 
System.out.println("Enter your name: "); 
String n=con.readLine();
System.out.println("Welcome "+n);  }} 


Enter your name: Anupam Mishra

Welcome Anupam Mishra


  Modified On Dec-14-2017 04:12:46 AM

Leave Comment