Java Scanner hasNextBoolean() method example

HQ » Java Tutorial » java.util » Scanner » hasNextBoolean() method example
Java Scanner hasNextBoolean() method example 2018-02-20T12:07:53+00:00

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 :

DataType Parameter Description
N/A N/A N/A

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 :

IllegalStateException

– 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 :

 

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close