java.util.InputMismatchException 通过读取 double

标签 java inputmismatchexception

当我尝试读取 0.6 时,我得到了 java.util.InputMismatchException 这里是代码的一部分。正如您所看到的,我尝试重新实现练习表的 SkipList。

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double f = scan.nextDouble();
        impl_with_errors list = new impl_with_errors(f);
        int n = scan.nextInt();


public class impl_with_errors {
    public static double chance;
    public Node list0;
    public Node list1;
    public Node list2;
    public Node list3;
/**
 * the constructor of the skiplist
 * @param p the chance that an element shall be in a higher list
 */
    public impl_with_errors(double p) {
        chance = p;
        list0 = null;
        list1 = null;
        list2 = null;
        list3 = null;
    }

最佳答案

来自文档:

public double nextDouble()

Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched.

解析 0.6 应该可以,但如果你想输入 0,6 你可以使用这个:

String dstr = scan.nextLine();

dstr = dstr.replace(",", ".");

double f = Double.parseDouble(dstr);

关于java.util.InputMismatchException 通过读取 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372814/

相关文章:

java - 扫描仪的输入不匹配异常

Java 加密 API 与不同平台

java - 部署 rest Web 服务时出错

java - 想要使用broadcastreceiver保存去电号码、持续时间

java - 在java中用正则表达式替换字符串

java - 如何使用 firebase 登录 token ?

java - 不能同时使用字符串和整数扫描器

Java扫描仪分隔符错误csv java行尾

java - 为什么当我输入非整数输入时我的代码会运行到无限循环?

java - 一个循环完美运行,而下一个相同的循环失败