Scanner hasNextInt() Method in Java

The hasNextInt method in Java

Well, sometimes, we are required to check whether or not the user has input some valid integer type data. In such situations, we can make use of the method hasNextInt, through which, the user will input something, and then it will be checked by this method, whether the given input is an integer or not. This method returns true, or false, depending upon what the input is from the user.

Checking if the user input is int type (hasNextInt method)

Let’s have a look at a program, where we are trying to demonstrate the use of the hasNextInt method.

hasnextint method in Java

package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean storesint = scanner.hasNextInt();
System.out.println(storesint);
}
}

This method returns true if the next input from the user is an integer, else it returns false. So, if you try to implement the above program, and give some integer number as an input, then it will return true, else it will return false.