我尝试从文件中读取双精度值并Scanner
以此为目标使用。
"input.txt" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
我不明白为什么会这样?
代码:
public class Largest
{
public static void main(String[] args)
throws FileNotFoundException
{
String filename = "input.txt";
Scanner in = new Scanner(filename);
double largest = in.nextDouble();
while (in.hasNextDouble())
{
double input = in.nextDouble();
if (input > largest)
{
largest = input;
}
}
in.close();
System.out.println("Largest value: " + largest);
}
}
更新:
我尝试更改double largest = in.nextDouble();
为double largest = 0;
但输入错误:
filename Actual Expected
-------------------------------------------------------------
"input.txt" Largest value: 0.0 Largest value: 1.343239923E9
"input2.txt" Largest value: 0.0 Largest value: 40.1
文件内容是这样的:
89343455
46746846
56.78
55486411
如何解决这个麻烦?