我试图从一个文本文件中拆分一些字符串,该文件在整数之间有制表符。整数 /t 整数 /t 整数。
50
1 2 46
1 15 38
1 16 43
1 21 4
1 25 24
1 35 2
1 43 6
我尝试使用字符串令牌但ArrayIndexOutOfBoundsException
出来了,所以我自己做了一些调试并发现了这一点。
我的代码如何:
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("Datas.txt"));
String data;
int i = 0;
while (input.hasNextLine()) {
data = input.nextLine();
String[] sp = data.split("\\t+");
String n1 = sp[0];
System.out.println("|" + n1 + "|");
}
input.close();
}
这是我编译并运行它时结果的图片链接。
编辑:不知道为什么结果会这样。切换到 bufferedReader 现在一切都很好。感谢人们的帮助。