Scanner hasNextShort() Method in Java

Scanner hasNextShort Java

If we are required to check whether or not, the data from the user input is of type short, we can make use of the hasNextShort method. This method returns true, if the input from the user is of type short, and otherwise, the method returns false.

Checking if the user input is short type(hasNextShort method)

Let’s have a look at a program, where we try to demonstrate the hasNextShort method →

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

So, if you try to implement the above program, you can find that the method returns true if the input from the user is in the range of short, else it returns false.