Java ScannerhasNextBoolean() Method
Example
Print the first boolean value that is found:
// Create a scanner objectScanner myObj = new Scanner("The value is false");// Skip tokens until a boolean is foundwhile (myObj.hasNext() && !myObj.hasNextBoolean()) { myObj.next();}// If there is a boolean then print itif (myObj.hasNextBoolean()) { System.out.print("The boolean value is "); System.out.println(myObj.nextBoolean());} else { System.out.println("No boolean found");}Definition and Usage
ThehasNextBoolean() method returnstrue if the next token represents a boolean value. A token represents a boolean value if its value matches one of the strings "true" or "false". The match is case-insensitive, which means that values like "True" and "FALSE" also represent a boolean value.
What is a token?
A token is a sequence of characters separated from other tokens by delimiters. The default delimiter is a block of whitespace characters but it can be changed with theuseDelimiter() method.
Syntax
public boolean hasNextBoolean()Technical Details
| Returns: | Aboolean value which if true the next token represents a boolean value. |
|---|---|
| Throws: | IllegalStateException - If the scanner has been closed. |

