Get User Input (using Scanner class)

In java, java.util.Scanner class is used to get user input and produce the result in the console. Here's a quick program to show you how to use Scanner class to read user input and print the result in the console.


import java.util.Scanner;

public class Main{
  public static void main (String[] args) {
    
    //Create a Scanner object 
    Scanner obj = new Scanner(System.in); //System.in it's an input stream and responsible to getting/reading user input
    
    //get an int value 
    System.out.println("Enter an int number");
    System.out.println("You typed "+obj.nextInt());
  }
}

When you run the above sample program in your system, you see the following message in the console:


Enter an int number (You will be promoted to type an int value)

10 
You typed 10

# Here, the Scanner class contains several methods to read user data/input. Like:

nextInt(): reads int data
nextFloat(): reads float data
nextDouble(): reads double data
nextLong(): reads long value
nextByte(): reads bytes
nextShort(): reads short data
nextBoolean(): reads boolean value
nextLine(): reads String data


# Let's check out another program where we get user input of various types:


import java.util.Scanner;
public class Main{
  public static void main (String[] args) {
    
    //Create a Scanner object 
    Scanner obj = new Scanner(System.in); //System.in it's an input stream and responsible to getting/reading user input
    
    //get user input
    System.out.println("Enter your name");
    System.out.println("My name is "+obj.nextLine());
    System.out.println("Enter your age");
    System.out.println("Your age is "+obj.nextInt());
    System.out.println("Enter your salary");
    System.out.println("My average salary is "+obj.nextDouble);
    System.out.println("Enter the PI vale");
    System.out.println("The PI value is "+obj.nextFloat());
    
    //add more
  }
}

After running the above sample example, do the following in the in the development console:


Enter your name
Mark Smith 
Your name is Mark Smith
Enter your age
25
My age is 25
Enter your salary
1200
My average salary is 1200.0
Enter the PI vale 
3.1416
The PI value is 3.1416

# User input as variable

Instead of printing direct user input, we can also create variables of different types and then access those variables.

Look at the following example:

In this example, we get three numeric variable from user keyboard and print summation.


import java.util.Scanner;

public class Main{
  public static void main (String[] args) {
    
    //Create a Scanner object 
    Scanner obj = new Scanner(System.in); //System.in it's an input stream and responsible to getting/readinguser input
    
    //Get user input
    System.out.println("Enter 3 numeric data (will be stored in x, y and z respectively");
    
    int x = obj.nextInt();
    int y = obj.nextInt();
    int z = obj.nextInt();
    //print summation
    int sum = x + y + z;
    System.out.println("Summation of x, y and z is "+ sum);
  }
}

In the console, you will be promoted to type three int value.


Input 3 numeric data 
10 
20
30
Summation of x, y and z is 60

Attention: Possibility to get InputMismatchException as an error, if the type doesn't match on your input value. So it is required to double check when you work with Scanner (user input) class.