3

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

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
4

0 回答 0