对作为扫描仪输入的 BigDecimals 进行验证

Validation on BigDecimals taken as input from Scanner

我正在从控制台接收用户输入,它需要是 BigDecimal。在程序中尝试使用它之前,如何验证它以确保它是这种类型?

以下是我阅读它的方式:scanner.nextBigDecimal()

您可以在读取值之前使用 hasNextBigDecimal 进行验证:

Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method.