Java Scanner nextBoolean() Method

The nextBoolean method

Well, sometimes, we are required to get the user input in the form of true, or false. In such a situation, we can make use of the next boolean method, with which, we can get boolean input from the user. For example, if the user is asked that is he/she above 18 years old, the answer will be either true or false. So, keeping this into account, we can use the next boolean method here.

Now, let’s move towards the implementation of nextBoolean() method →

Get boolean data as user input(nextBoolean method)

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.nextBoolean();
System.out.println(storesboolean);
}
}

So, if you implement this program and try giving some weird input instead of true or false, you will get an error, since your input does not match true or false. If you give input like true or false, everything will be fine.