In Java, sc.hasNextBoolean() checks whether the next delimited token matches "true" or "false" (ignoring letter case), without advancing the scanner.
Other common yes/no forms such as "yes", "no", "1", or "0" return false.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.hasNextBoolean(...).
Syntax
Check strict boolean tokens with hasNextBoolean() before calling nextBoolean().
Java
Scanner sc = new Scanner("TRUE maybe false");
boolean a = sc.hasNextBoolean(); // true
Call
What it does
sc.hasNextBoolean()
Returns true for case-insensitive "true" or "false".
sc.nextBoolean()
Consumes the verified boolean token.
hasNextBoolean() is completely independent of locale and radix settings.
Library declarations
Java
// java.util.Scanner
public boolean hasNextBoolean()
Parameters
Parameter
Description
None
hasNextBoolean() takes no parameters.
Return Value
Return Value
Description
boolean
true if the next token equals "true" or "false" ignoring case.