In Java, sc.hasNextBigDecimal() checks whether the next token represents a valid finite decimal number for nextBigDecimal() under the scanner's current locale().
Use it to validate invoice prices, tax rates, or exact decimal amounts before adding them without throwing InputMismatchException.
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.hasNextBigDecimal(...).
Syntax
Set Locale.US when validating dot-decimal input, then guard reads with hasNextBigDecimal().
Java
Scanner sc = new Scanner("10.25 TBD 4.75").useLocale(Locale.US);
while (sc.hasNext()) {
if (sc.hasNextBigDecimal()) { /* safe to call sc.nextBigDecimal() */ }
}
Call
What it does
sc.hasNextBigDecimal()
Returns true if the token is a finite decimal in the current locale.
sc.useLocale(Locale.US)
Sets dot-decimal syntax regardless of OS default locale.
Unlike hasNextDouble(), hasNextBigDecimal() returns false for "NaN" and "Infinity" because BigDecimal represents only finite numbers.
Library declarations
Java
// java.util.Scanner
public boolean hasNextBigDecimal()
Parameters
Parameter
Description
None
hasNextBigDecimal() takes no parameters.
Return Value
Return Value
Description
boolean
true if the token is a valid finite decimal in the scanner's locale; false otherwise.