How to read int value using Scanner Class Java

If you want to read input from the user in a Java Application, you should make use of the Scanner class file from java.util package.

If you specifically want to read an int value, then you can make use of the nextInt() method from the Scanner class.



import java.util.Scanner;

public class ReadIntegerScannerProgram {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(;
        System.out.print("Enter an int value: ");

        int inputtedIntValue = scanner.nextInt();

        System.out.println("Entered int value: " + inputtedIntValue);


Enter an int value: 20
Entered int value: 20

Note: You will get an InputMismatchException if the user inputted value is not of type int or Integer.

Enter an int value: hello
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(
	at java.util.Scanner.nextInt(
	at java.util.Scanner.nextInt(

Have Questions? Post them here!