Home Java Scanner Methods hasNextDouble() Java Scanner hasNextDouble() Method Definition In Java, sc.hasNextDouble() checks whether the next token is formatted as a valid double under the scanner's active locale() without consuming it.
It returns true for whole numbers (10), decimal numbers (3.5 in Locale.US), scientific notation (1.2E3), and NaN or Infinity.
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.hasNextDouble(...).
Locale affects dot vs comma recognition. Before parsing dot-separated numbers like "4.50", call sc.useLocale(Locale.US); otherwise hasNextDouble() returns false in locales that expect decimal commas (such as German or French).
Syntax Configure Locale.US when needed, then check hasNextDouble() inside a loop or conditional.
Scanner sc = new Scanner("4.50 free 12.25").useLocale(Locale.US);
if (sc.hasNextDouble()) {
double price = sc.nextDouble();
} Call What it does sc.hasNextDouble()Returns true if the next token parses as a double in the scanner locale. sc.useLocale(Locale.US)Ensures dot-decimal numbers return true everywhere.
hasNextDouble() never throws InputMismatchException. It simply returns false when the next token fails decimal syntax or when input has ended.
Library declarations // java.util.Scanner
public boolean hasNextDouble() Parameters Parameter Description None hasNextDouble() takes no parameters.
Return Value Return Value Description booleantrue if the token matches the locale's floating-point pattern; false otherwise.Throws IllegalStateException If the scanner has been closed.
Important
Notice that hasNextDouble() returns true for integers like "42" as well as "NaN" and "Infinity". If you want to reject NaN and Infinity, test !Double.isNaN(v) && !Double.isInfinite(v) after reading—or use hasNextBigDecimal() , which only accepts finite numbers.
Compare with Similar Methods Floating-point lookahead compared:
Java Examples 1. Sum mixed prices 2. US vs German peek 3. NaN, Infinity, exponents Example 1 — Sum Valid Prices While Skipping Words Like "free" Check each token with hasNextDouble() so non-numeric words like "free" are skipped smoothly.
import java.util.Locale;
import java.util.Scanner;
public class ScannerHasNextDoublePrices {
public static void main(String[] args) {
String receipt = "4.50 free 12.25 discount 3.25";
double total = 0.0;
try (Scanner sc = new Scanner(receipt).useLocale(Locale.US)) {
while (sc.hasNext()) {
if (sc.hasNextDouble()) {
total += sc.nextDouble();
} else {
System.out.println("Skipping word: " + sc.next());
}
}
}
System.out.println("Total: " + total);
}
} Skipping word: free
Skipping word: discount
Total: 20.0 How It Works Guaranteed dot parsing. useLocale(Locale.US) prevents "4.50" from failing on non-English systems.Non-numbers routed safely. "free" and "discount" return false and are consumed via sc.next().Clean binary total. 4.50 + 12.25 + 3.25 sums cleanly to 20.0.Example 2 — Compare hasNextDouble() Under US and German Locales Test the exact same unconsumed token "3,5" before and after switching locale.
import java.util.Locale;
import java.util.Scanner;
public class ScannerHasNextDoubleLocale {
public static void main(String[] args) {
try (Scanner sc = new Scanner("3,5").useLocale(Locale.US)) {
System.out.println("US sees 3,5 as double? " + sc.hasNextDouble());
sc.useLocale(Locale.GERMANY);
System.out.println("DE sees 3,5 as double? " + sc.hasNextDouble());
System.out.println("Parsed value: " + sc.nextDouble());
}
}
} US sees 3,5 as double? false
DE sees 3,5 as double? true
Parsed value: 3.5 How It Works Failed peek keeps token ready. Returning false under Locale.US leaves "3,5" waiting at the front of the scanner.Locale switch unlocks it. Switching to Locale.GERMANY flips hasNextDouble() to true on that exact same token.Standard Java output. Once read by nextDouble(), Java prints the numeric value as 3.5.Example 3 — Test hasNextDouble() on Exponents, NaN, and Infinity Compare hasNextDouble() with hasNextBigDecimal() on scientific notation and special values.
import java.util.Locale;
import java.util.Scanner;
public class ScannerHasNextDoubleSpecial {
public static void main(String[] args) {
String tokens = "1.2E2 NaN Infinity";
try (Scanner sc = new Scanner(tokens).useLocale(Locale.US)) {
while (sc.hasNext()) {
boolean asDbl = sc.hasNextDouble();
boolean asDec = sc.hasNextBigDecimal();
System.out.println(sc.next() + " -> double=" + asDbl + ", BigDecimal=" + asDec);
}
}
}
} 1.2E2 -> double=true, BigDecimal=true
NaN -> double=true, BigDecimal=false
Infinity -> double=true, BigDecimal=false How It Works Exponents pass both. Scientific notation 1.2E2 is accepted by both hasNextDouble() and hasNextBigDecimal().IEEE specials pass double only. NaN and Infinity are valid doubles, returning true only for hasNextDouble().Peek twice before advancing. Calling both lookahead checks before sc.next() lets us compare them on identical tokens.Frequently Asked Questions Does hasNextDouble() return true for whole integers like "10"? Yes. Any valid integer can also be read as a double (10.0), so hasNextDouble() returns true.
Why does hasNextDouble() return false on "3.5" on some computers? The scanner uses the JVM default locale, where some regions use commas (3,5) instead of dots. Call sc.useLocale(Locale.US) for dot decimals.
Does hasNextDouble() return true for "NaN" and "Infinity"? Yes. Scanner accepts NaN and Infinity as floating-point tokens for nextDouble().
Does hasNextDouble() advance the scanner? No. It only inspects the next token without consuming it.
Does hasNextDouble() have a radix parameter? No. Floating-point numbers in Scanner are always base 10.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion