bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java Scanner hasNextFloat() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Scanner hasNextFloat() 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 probability is 45.6 percent");
3Order

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

Print the value of every floating point number in the string:
Definition and Usage
Java Scanner hasNextFloat() Method

❮ Scanner Methods

Example

Print the value of every floating point number in the string:

// Create a scanner object Scanner myObj = new Scanner("The probability is 45.6 percent");
// Print the value of every floating point number in the scanner
while (myObj.hasNext()) {
 if (myObj.hasNextFloat()) {
 System.out.println(myObj.nextFloat());
 } else {
 myObj.next();
}
}

Definition and Usage

The hasNextFloat() method returns true if the next token represents a valid number.

The scanner is able to interpret digit groupings, such as using a comma for separating groups of 3 digits. The format of the groupings and the character used as a decimal point depend on the locale settings of the scanner, which can be changed with the useLocale() method.

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 hasNextFloat()

Technical Details

Returns:A boolean value which is true if the next token represents a valid number.
Throws:IllegalStateException - If the scanner has been closed.

❮ Scanner Methods

Previous

Java Scanner hasNextDouble() Method

Next

Java Scanner hasNextInt() Method