gpt4 book ai didi

java.lang.ArrayIndexOutOfBoundsException :10 even though the array can go up to 10

转载 作者:行者123 更新时间:2023-12-02 05:08:54 24 4
gpt4 key购买 nike

即使我从中获取数据的文件的索引足以为 10,我在“if”语句中收到“java.lang.ArrayIndexOutOfBoundsException:10”错误。

当我在代码中的其他地方调用 info[10] 时,它可以工作,但我不知道为什么它在这里不起作用。

现在正在学习计算机科学中的文件管理单元...

public static void comSci(String onTheMap) throws IOException  
{
BufferedReader input = new BufferedReader (new FileReader ("data.txt"));
if (onTheMap.equals("3")){
Scanner scanner = new Scanner("data.txt");
String line="x";
System.out.println("--------------------------------------------------------------");
System.out.println("Create a file of student's who are enrolled in ICS3U0:");
System.out.println("--------------------------------------------------------------");
String info[] = new String[20];
boolean finder = false;

while (line!=null) {
line = input.readLine();
if (line==null)
break;
info = line.split(",");

if (info[10].toLowerCase().contains("ICS3U0".toLowerCase())) { //PROBLEM
finder = true;
String programmers = info[0] + "," + info[1];
System.out.println(programmers);
try {
FileWriter theFile = new FileWriter("ICS3U0.txt",true);
PrintWriter names = new PrintWriter(theFile);
names.println();
names.close();

}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
System.out.println("ICS3U0.TXT WAS CREATED");

}
input.close();

}

最佳答案

Java 数组索引从 0 开始。因此长度为 10 的数组具有 0-9(含 0-9)(或不含 0-10)的有效索引。这就是为什么 for 循环通常被设置样式,

for (int i = 0; i < arr.length; i++) { // <-- less than (not less than =).
// ...
}

关于java.lang.ArrayIndexOutOfBoundsException :10 even though the array can go up to 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27514850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com