Java扫描程序抛出InputMismatchException以尝试读取nextDouble()

时间:2016-01-10 01:41:08

标签: java file exception double inputmismatchexception

我试图从文本文件中读取一个整数和两个双精度数。我写了一个简短的示例演示我的问题。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int q = 0;
        double w = 0, e = 0;
        Scanner s = null;
        try {
            s = new Scanner(new File(args[0]));
            while(s.hasNext()) {
                q = s.nextInt();
                w = s.nextDouble();
                e = s.nextDouble();
            }
            System.out.println("1: " + q + "\n2: " + w + "\n3: " + e);
        } catch(FileNotFoundException e1) {
            e1.printStackTrace();
            System.out.println(e1.getMessage());
        } finally {
            if(s != null) {
                s.close();
            }
        }
    }
}

它给了我一个

    Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Unknown Source)
        at java.util.Scanner.next(Unknown Source)
        at java.util.Scanner.nextDouble(Unknown Source)
        at Test.main(Test.java:14)

我认为我的扫描仪在阅读双重内容时遇到了问题,但我无法弄清楚原因。

我的文字文件包含:

    3000 30.3 10.4

0 个答案:

没有答案