gpt4 book ai didi

java - 线程 "main"java.util.InputMismatchException nextDouble() 错误中出现异常?

转载 作者:行者123 更新时间:2023-12-02 03:48:18 24 4
gpt4 key购买 nike

CitireFisier.java

public class CitireFisier  {
public static void main(String[] args) {
File f = new File("fisier.txt");

Scanner scn = null;
try {
scn = new Scanner(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

int size = scn.nextInt();
System.out.println("val is " + size);

double var = scn.nextDouble();
System.out.println("val is " + var);
}
}

输出

val is 3
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)

fisier.txt

3
0.1 0.7 0.2 init g
0.0 0.0 1.0 g y
0.0 0.0 1.0 g y

nextDouble()收到错误,但 0.1 是 double !

最佳答案

如果您的语言环境使用逗号作为小数点分隔符,则 0.1 不是 double 。

要解决此问题,请像这样实例化您的扫描仪:

 scn = new Scanner(f).useLocale(Locale.US);

关于java - 线程 "main"java.util.InputMismatchException nextDouble() 错误中出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36129792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com