JAVA- 读取文件 1 时出错(错误)

JAVA- Error reading file 1 (error)

我正在尝试读取一个 txt 文件并将其保存在一个数组中。 txt文件格式如下:

A B 5
A C 2
A D 4
.....

public class search {
    public static void main(String[] args) throws ParseException {
        try {  

            Scanner user_input = new Scanner(System.in); // for user input            
            System.out.println("Enter the file name: ");
            String filename1 = user_input.next();

            File file = new File(filename1);

            search bd = new search();
            Node[] nodes;
            nodes = bd.getNodes(file);
            bd.printNodes(nodes);

        }
        catch(Exception e) {
            System.out.println("Error reading file " + e.getMessage());
        } 
    }    

    public Node[] getNodes(File file) throws IOException {
        FileReader bd = new FileReader(file);
        BufferedReader bufferReader = new BufferedReader(bd);
        String line;
        ArrayList<Node>list = new ArrayList<Node>(); 
        while ((line = bufferReader.readLine()) != null) { 
            String[] token = line.split(" "); // create string of tokens
            list.add(new Node(token[0], token[1], Integer.parseInt(token[2])));

        }
        bufferReader.close(); 

        return list.toArray(new Node[list.size()]); // converting list to array
    }

    public void printNodes(Node[] nodes) {
        System.out.println("======================");

        for(Node node : nodes) {
            System.out.println(node);
        }
        System.out.println("======================");
    }

以下是我的节点class

class Node {

    String leftchild;
    String rightchild;
    int cost;

    public Node(){

    }

    public Node(String firstchild, String secondchild, int cost){
        this.leftchild = firstchild;
        this.rightchild = secondchild;
        this.cost = cost;
    }

    public Node(String firstchild, String secondchild) {
        this.leftchild = firstchild;
        this.rightchild = secondchild;
    }

    public ArrayList<String> getChildren(){
        ArrayList<String> childNodes = new ArrayList<String>();
        if(this.leftchild != null)
        {
            childNodes.add(leftchild);
        }
        if(this.rightchild != null) {
            childNodes.add(rightchild);
        }
        return childNodes;
    }

    public boolean removeChild(Node n){
        return false;
    }

    @Override
    public String toString() {
        return leftchild +" "+ rightchild;
    }

}

我没有编译问题,但是当我 运行 我的代码时,它给我错误

error reading file 1

不知道为什么。我尝试以多种方式更改我的代码,但其中 none 行得通。谁能帮我解决这个问题? 谢谢

如果您得到 ArrayIndexOutOfBoundsException: 1,这意味着您的测试输入文件中至少有一行不包含任何 space。

确实在你的代码中你做了 String[] token = line.split(" ") 这将提取所有由 space 分隔的标记并将它们放入 String 的数组中,所以如果你得到这个错误它意味着数组的 length1 这样你就无法访问 token[1] 因为它指的是 array 的第二个元素,它只包含一个元素。

因此,在您的情况下,您应该首先测试数组的 length 是否具有正确的 length 作为下一个:

String[] token = line.split(" "); // create string of tokens
if (token.length >= 3) {
    list.add(new Node(token[0], token[1], Integer.parseInt(token[2])));
}