Receiving input from user -


Three different methods to take input from user keyboard are as follows -

1- Using readLine() method of java.io.Console class -

It is one of the simplest method to take input from user keyboard added in jdk1.6 using Console class.
Console class has a method named readLine(), which read a complete line and return the value in String format.
As we already know, to access methods of a class we need to create object of the class. We can create object of Console class as follows -
Console c = System.console(); 
After creating object we can access its readLine() method by using . (period) sign as follows -
s = c.readLine("Enter input");
Or we can combine these lines into one as follows -
String s = System.console().readLine("Enter input");
Note -
We have passed a String parameter in readLine() function and it is optional, if you pass any message then the message will be displayed first and then it takes input value, if you do not pass any message then it directly takes user input.

Example -

Program for the addition of two numbers input by the user -

public class Demo1
{
 public static void main(String args[]) 
 {
  int a,b,c;
          
  String s1 = System.console().readLine("Enter first number");
  a = Integer.parseInt(s1);
   
  /* We can combine above two line into one as follows - 
     a = Integer.parseInt(System.console().readLine("Eneter first number"));
  */
  b = Integer.parseInt(System.console().readLine("Eneter second number"));
  c = a+b;
  System.out.println("Sum = "+c);
  }
 }
     
          
 

Output -

 Enter first number
 10
 Enter second number
 20
 Sum = 30
Note - This method always return String, So we can use parse() method provided by wrapper classes to convert String into desired data type.

2.Using java.util.Scanner class -

Scanner class available in java.util package, provides various methods to take input from user keyboard -
Method Description
public String nextLine() It reads and returns a complete line at once and moves the Scanner position to next line.
public byte nextByte() It reads and return next input token as byte. An InputMismatchException will be thrown if token cannot be translated into valid byte value or value is out of range of byte.
public short nextShort() It reads and return next input token as short. An InputMismatchException will be thrown if token cannot be translated into valid short value or value is out of range of short.
public int nextInt() It reads and return next input token as int. An InputMismatchException will be thrown if token cannot be translated into valid int value or value is out of range of int.
public long nextLong() It reads and return next input token as long. An InputMismatchException will be thrown if token cannot be translated into valid long value or value is out of range of long.
public float nextFloat() It reads and return next input token as float. An InputMismatchException will be thrown if token cannot be translated into valid float value or value is out of range of float.
public double nextDouble() It reads and return next input token as double. An InputMismatchException will be thrown if token cannot be translated into valid double value or value is out of range of double.
public boolean nextBoolean() It reads and return next input token as boolean. An InputMismatchException will be thrown if token cannot be translated into valid boolean value.

So to take input, first create object of Scanner class as follows -
Scanner sc = new Scanner(System.in);
//System.in represents standard input stream typically connected to user keyboard.
     
Next call any of the method defined in above table, depending upon your needs -
For Example -
int a = sc.netInt();  // To read integer input
String str = sc.nextLine();  //  To read a line as String
float b = sc.nextFloat();    //  To read float input 

Example -

Program for the addition of two numbers input by the user -

import java.util.Scanner;
public class Demo2
{
 public static void main(String args[]) 
 {
  int a,b,c;
          
  System.out.println("Enter first number");
  Scanner sc= new Scanner(System.in);
  a = sc.nextInt();
  System.out.println("Enter second number");
  b = sc.nextInt();
  c = a+b;
  System.out.println("Sum = "+c); 
 }
}
     
         

Output -

Enter first number
10
Enter second number
20
Sum = 30


3.Using java.io.BufferedReader class -

BufferedReader class available in java.io package, provides readLine() method to take input from user keyboard.
First of all create object of InputStreamReader class -
InputStreamReader isr = new InputStreamReader(System.in);
    
Next connect this InputStreamReader stream to BufferdReader object -
BufferedReader br = new BufferedReader(isr);    
    
Finally call readLine() method on BufferedReader object -
String str = br.readLine();  // throws IOException
    
Note - This readLine() method is different from the method we learned in Console class. It cannot have string massage within it. And it also throws IOException.

Example -

Program for the addition of two numbers input by the user -

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

public class Demo3
 {
  public static void main(String args[])throws IOException
  {
   int a,b,c;
          
   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
   System.out.println("Enter first number");
   String str1 = br.readLine();   
   a=  Integer.parseInt(str1);
       
   System.out.println("Enter second number");
   String str2 = br.readLine();
   b = Integer.parseInt(str2);
   c = a+b;
   System.out.println("Sum = "+c);  
   }
  }
    
    
    

Output -

Enter first number
10
Enter second number
20
Sum = 30
 
Note - This readLine() throws IOException. So to handle it we made main() method to throws IOException.