Scanner hasNextByte() method in Java

hasNextByte Method

If we are required to check whether the given data entered by the user, is of type byte, then we can make use of the hasNextByte method. This method returns true, if the user input was a byte type, and else, the method gives false.

Checking if the user input is byte type(hasNextByte method)

Let’s now have a look at a program, where we are demonstrating the hasNextByte method.

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

So, if you implement the above program and give some input which is a byte type, then you will find the output to be true, else the output will be false. You can try implementing the program for various values.