Flash cards
Review the key moves
What is the main idea behind Java Scanner hasNextBoolean() Method?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
// ___ a scanner object Scanner myObj = new Scanner("The value is false");Put the learning moves in the order that makes the concept easiest to apply.
❮ 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 hasNextBoolean() method returns true 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 the useDelimiter() method.
Syntax
public boolean hasNextBoolean()Technical Details
| Returns: | A boolean value which if true the next token represents a boolean value. |
|---|---|
| Throws: | IllegalStateException - If the scanner has been closed. |
❮ Scanner Methods