Why is nextDouble() from the Scanner method sending me “Exception”

五迷三道 提交于 2019-12-02 10:38:06

It is dependant on Locale, try to use comma instead of a dot or vice versa.

Ex: 1,5 instead of 1.5

You can check, if there is some int or double to read. And you have to use , or . depending on the country, you are. If you need it country independent, read it as string and parse then (see below)

A solotion would be to read the line as a string and parse it then to int and double.

Checking if double is available:

input.hasNextDouble();

Read as String:

String line = input.nextLine();
String[] sl = line.split(" ");
amount = Integer.parseInt(sl[0]);
balance = Double.parseDouble(sl[1]); //solve the problem with . and ,

You also could check if there are enough inputs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!