首页 > 解决方案 > 从文件中读取交替数据类型

问题描述

我正在尝试将文件中的坐标读取到数组中。

每个坐标都有一个 id、anx-value和 a y-value

该文件采用以下格式:id position.x position.y

例子:

292961234 1376.42 618.056
29535583 3525.73 530.522
256351971 836.003 3563.33
20992560 4179.74 3074.27

注意:共有 4 行,共包含 4 个坐标。

我创建了类Node和它的构造函数期望(int id, double x, double y)

这是 NodeList 类,它应该具有保存节点的数组属性:

package .....;

import java.io.File;
import java.util.Iterator;
import java.util.Locale;
import java.util.Scanner;

public class NodeList implements Iterable<Node> {
    private Node[] nodes = getNodes();
    @Override
    public Iterator<Node> iterator() {
        return null;
    }
    public Node[] getNodes() {
        Node[] result;
        File f;
        Scanner scanner;
        try {
            f = new File("nodes.txt");
            Scanner s = new Scanner(f);
            int ctr = 0;
            while (s.hasNextLine()) {
                ctr++;
                s.nextLine();
            }
            result = new Node[ctr];
        }
        catch (Exception e) {
            return null;
        }
        try {
            scanner = new Scanner(f);
        }
        catch (Exception e) {
            return null;
        }
        Locale.setDefault(new Locale("C"));
        for(int i = 0; i < result.length; i++) {
            int id = scanner.nextInt();
            double x = scanner.nextDouble();
            double y = scanner.nextDouble();
            result[i] = new Node(id,x,y);
        }
        return result;
    }
    public static void main(String[] args) {
        NodeList nl = new NodeList();
    }
}

Node班级:

package ...;

public class Node {
    private int id;
    private double x;
    private double y;
    public Node(int id, double x, double y){
        this.id = id;
        this.x = x;
        this.y = y;
    }
    public int getId(){
        return id;
    }
    public double getX(){
        return x;
    }
    public double getY(){
        return y;
    }
}

当调用包含所示代码的方法时,我得到以下 异常:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at ......NodeList.getNodes(NodeList.java:40)
    at ......NodeList.<init>(NodeList.java:9)
    at ......NodeList.main(NodeList.java:47)

Process finished with exit code 1

链接到包含节点的文件:https ://pastebin.com/zhzp3DTi

标签: javajava.util.scanner

解决方案


可能是它使用了错误的语言环境。我不认为“C”是一个有效的语言环境,它应该是一个语言代码。

你可以尝试使用

scanner.useLocale(Locale.ROOT);

这在我的机器上工作:

public static void main(String[] args) {
    // TODO code application logic here
    String s = "292961234 1376.42 618.056\n" +
               "29535583 3525.73 530.522\n" +
               "256351971 836.003 3563.33\n" +
               "20992560 4179.74 3074.27";
    Scanner scanner = new Scanner(s);
    scanner.useLocale(Locale.ROOT);
    for(int i = 0; i < 4; i++) {
        int id = scanner.nextInt();
        double x = scanner.nextDouble();
        double y = scanner.nextDouble();
        System.out.println(id+" "+x+" "+y);
    }
}

推荐阅读