In Java, sc.nextDouble() reads the next token and converts it into a 64-bit IEEE 754 double. It accepts plain decimals like 3.5, whole numbers like 10 (returned as 10.0), scientific notation like 1.5E3, and special tokens NaN and Infinity.
Locale matters:nextDouble() expects either a dot (3.5) or a comma (3,5) as the decimal separator depending on the scanner's locale().
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.nextDouble(...).
Always specify Locale.US for dot decimals.
A scanner starts with the JVM's default locale. On a German or French machine, new Scanner("3.5").nextDouble() fails with InputMismatchException unless you call sc.useLocale(Locale.US) first.
Syntax
Chain .useLocale(Locale.US) when parsing dot-decimal inputs before calling nextDouble().
Parses dot-decimal input reliably regardless of OS region.
sc.hasNextDouble()
Checks whether the next token is a valid double in the current locale.
sc.nextDouble()
Parses a decimal number, exponent, or NaN/Infinity.
Unlike integer methods, nextDouble() has no radix overload—floating-point scanner tokens are always base 10. For exact currency calculations where binary rounding like 0.1 + 0.2 causes drift, prefer nextBigDecimal().
Library declarations
Java
// java.util.Scanner
public double nextDouble()
Parameters
Parameter
Description
None
nextDouble() takes no parameters.
Return Value
Return Value
Description
double
The parsed 64-bit floating-point number.
Throws InputMismatchException
If the token does not match the current locale's decimal format.
US rejected 3,5: InputMismatchException
DE 3,5 -> 3.5
DE 1.234,5 -> 1234.5
How It Works
US rejects decimal comma. Under Locale.US, "3,5" has only one digit after the comma (not three for grouping), so nextDouble() throws without advancing.
Switch locale mid-stream. Calling useLocale(Locale.GERMANY) updates the parser rules immediately without discarding "3,5".
Java always prints dots. Even after reading German notation "1.234,5", Java's Double.toString prints 1234.5.
Example 3 — Parse Scientific Notation, NaN, and Infinity
Verify that nextDouble() understands exponential notation and IEEE special floating-point words.
Java
import java.util.Locale;
import java.util.Scanner;
public class ScannerNextDoubleSpecial {
public static void main(String[] args) {
String tokens = "1.5E3 2.50 NaN -Infinity";
try (Scanner sc = new Scanner(tokens).useLocale(Locale.US)) {
while (sc.hasNextDouble()) {
System.out.println(sc.nextDouble());
}
}
}
}
Output
1500.0
2.5
NaN
-Infinity
How It Works
Exponent expansion.1.5E3 means 1.5 times 1000, which prints in standard decimal notation as 1500.0.
Trailing zeros normalize."2.50" stores the numeric value 2.5 and drops the extra trailing zero when printed.
Special IEEE tokens. Both "NaN" and "-Infinity" match hasNextDouble() and parse into their double constants.
Frequently Asked Questions
If your system default locale uses commas for decimals (such as German or French), Scanner expects 3,5 instead of 3.5. Call sc.useLocale(Locale.US) before reading.
No. Floating-point inputs in Scanner are always parsed in base 10.
nextDouble() stops before the newline character, leaving it in the buffer. Call sc.nextLine() once to discard the leftover newline before reading the next real line.
Yes. "10" parses as the double 10.0.
Prefer nextBigDecimal() for exact money math so binary floating-point rounding (like 0.1 + 0.2 = 0.30000000000000004) never alters cents.