If we are required to check whether or not, the data entered by the user is of the type boolean, we can use the hasNextBoolean method. Simply, we are checking if the data entered by the user is boolean or not.
Checking if the user input is boolean type(hasNextBoolean method)
This method returns true if the input from the user is true or false, else it returns false. Let’s now have a look at a program, where we try demonstrating the hasNextBoolean method. Have a look.
Please refer the below program to understand the implementation.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean storesboolean = scanner.hasNextBoolean();
System.out.println(storesboolean);
}
}
If you try implementing the above program and give ”true” or “false” as an input, then it will give true as an output. Any other input other than these two will straightway produce false as an output.