bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java Scanner nextBoolean() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Scanner nextBoolean() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ a scanner object Scanner myObj = new Scanner("The value is false");
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Print the first boolean value that is found:
Definition and Usage
Java Scanner nextBoolean() Method

❮ Scanner Methods

Example

Print the first boolean value that is found:

// Create a scanner object Scanner myObj = new Scanner("The value is false");
// Skip tokens until a boolean is found
while (myObj.hasNext() && !myObj.hasNextBoolean()) {
 myObj.next();
}
// If there is a boolean then print it
if (myObj.hasNextBoolean()) {
 System.out.print("The boolean value is ");
 System.out.println(myObj.nextBoolean());
} else {
System.out.println("No boolean found");
}

Definition and Usage

The nextBoolean() method returns the boolean value that the next token represents. 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 the useDelimiter() method.

Syntax

public boolean nextBoolean()

Technical Details

Returns:The boolean value that the next token represents.
Throws:InputMismatchException - If the token does not represent a boolean value. NoSuchElementException - If there are no more tokens in the scanner. IllegalStateException - If the scanner has been closed.

❮ Scanner Methods

Previous

Java Scanner next() Method

Next

Java Scanner nextByte() Method