java.util.Scanner hasNextBoolean()

Description :

This java tutorial shows how to use the hasNextBoolean method of Scanner class of java.util package. This method checks if the Scanner object has boolean data type on its buffer.

Method Syntax :

public boolean hasNextBoolean()

Parameter Input :


Method Returns :

This method simply returns true if the next token string on the scanner buffer can be interpreted as boolean data type. If the Scanner object token is either “true” or “false” the Scanner returns true otherwise false.

Compatibility Version :

Requires Java 1.5 and up

Exception :


– The IllegalStateException will be thrown by this Scanner object if and only if we have invoked the method hasNextBoolean after the scanner object has been closed.

Discussion :

The method hasNextBoolean is simply to check if the token on the scanner object can be interpreted as boolean. This can be used as flag in scanning the next token of this scanner.

Java Code Example :

This java example source code demonstrates the use of hasNextBoolean method of Scanner class.

File Contents

Sample Output :

Running the hasNextBoolean() method example source code of Scanner class will give you the following output

java scanner hasnextboolean method example

java scanner hasnextboolean method example

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :