java关于ArrayIndexOutOfBoundsException的问题?
所以我有这个密码。。。这就是它的一部分
File File = new File("data2.txt");
Scanner readUpdate = new Scanner(File);
Player[] updatePlayers = new Player[200];
String updateSTR;
int updateTotalCounter = 0;
while (readUpdate.hasNext()) {
updateSTR = readUpdate.nextLine();
String [] updateData = updateSTR.split(",");
updatePlayers[updateTotalCounter] = new Player(updateData[0], updateData[1],updateData[2],
Integer.parseInt(updateData[3]), Integer.parseInt(updateData[4]));
updateTotalCounter++;
}
readUpdate.close();
Java不断出现并告诉我这一点
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Main.main(Main.java:43)
我不明白这是什么意思。有什么线索吗
# 1 楼答案
最好检查数组的长度
# 2 楼答案
这意味着您试图访问数组中大于数组中元素数的位置,因此会出现常见的异常
您假设数组updateData对于您读取的每一行都有5个位置,而对于至少一行来说,这似乎是错误的,因此抛出异常
确保
String [] updateData = updateSTR.split(",");
所有线条的尺寸为5(从0到4)。通过这种方式解决问题,但请注意,某些行将不会被处理: