我试图通过使用分隔符来分隔不同的字段,将文本文件读入二叉树。当我尝试将其读入二叉树时,我得到的数据超出界限erropackage Hospital;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;`
public class main {
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("patient.txt"));
String line;
BinaryTree hospital = new BinaryTree();
while ((line = in.readLine()) != null) {
String[]text = line.split(",");
hospital.insert(text[0], text[1], text[2], text[3], text[4]);
}
in.close();
}
}
答案 0 :(得分:3)
使用以下代码更改您的拆分代码。如果不使用limit参数,则默认情况下,split方法会删除尾随空元素。使用-1
参数,此问题将得到解决。
String[] text = line.split(",", -1);