Java.util.Scanner.nextBoolean() Method

Description

The java.util.Scanner.nextBoolean() method scans the next token of the input into a boolean value and returns that value. This method will throw InputMismatchException if the next token cannot be translated into a valid boolean value. If the match is successful, the scanner advances past the input that matched.

Declaration

Following is the declaration for java.util.Scanner.nextBoolean() method

public boolean nextBoolean()

Parameters

NA

Return Value

This method returns the boolean scanned from the input

Exception

InputMismatchException -- if the next token is not a valid boolean

NoSuchElementException -- if the input is exhausted

IllegalStateException -- if this scanner is closed

Example

The following example shows the usage of java.util.Scanner.nextBoolean() method.