how to check the data type validity of user's input (Java Scanner class)

前端 未结 5 1391
情话喂你
情话喂你 2020-12-17 07:29

I am trying to make a simple UI that asks users to input double-type numbers, if theirs input is not of double type, the program should keep printing the prompt until user i

5条回答
  •  攒了一身酷
    2020-12-17 07:48

    What about Double.parseDouble(stringInput); when you scan the input as a String you can then parse it to see if it is a double. But, if you wrap this static method call in a try-catch statement, then you can handle the situation where a double value is not parsed.

提交回复
热议问题