Java Scanner nextDouble() Method
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.hasNextDouble()) {
System.out.println(myObj.nextDouble());
} else {
myObj.next();
}
}
Definition and Usage
The nextDouble()
method returns a double
value containing the number represented by the next token.
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 double nextDouble()
Technical Details
Returns: | A double value containing the number represented by the token. |
---|---|
Throws: |
InputMismatchException - If the token does not represent a valid number.NoSuchElementException - If there are no more tokens in the scanner.IllegalStateException - If the scanner has been closed.
|