[英]java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0; why a[0][0] has ArrayIndexOutOfBoundsException problem?
[英]java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 error is being received and cannot confirm why
我了解此错误消息表明我正在尝试通过不存在的索引访问数组中的特定元素,或者它是试图访问字符串长度之外的无效元素。 但是,我无法使用错误消息中提供的信息来确认我的代码中究竟发生了这种情况:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Week3.luis_ramirez_GamesReport.readWrite(luis_ramirez_GamesReport.java:63) at Week3.luis_ramirez_GamesReport.main(luis_ramirez_GamesReport.java:30)
我已经查看了以前发布的有关此问题的问题,但我的代码格式似乎与已发布的其他问题不相似。 我试图更改我的 output 的顺序,但由于所需的 output 已经在打印,我停止这样做。 我还尝试更改行String[] ints_only = Arrays.copyOfRange(record, 1, record.length);
到 0 而不是 1 的值。
这是我的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class luis_ramirez_GamesReport {
public static void main(String[] args) throws IOException
{
{
File fileName = new File("/Users/luisramirez/eclipse-workspace/GameScores.txt");
readWrite(fileName);
addGamer(fileName, "Jimmy", "189", "190", "197", "199", "198", "193", "199", "199", "188", "196");
readWrite2(fileName);}
}
private static void readWrite(File fileName) throws IOException {
if (fileName.exists())
{
BufferedReader br = null;
String line = "";
String csvSplitBy = ",";
int recordCount = 0;
//String number ="167";
//int result = Integer.parseInt(number);
br = new BufferedReader(new FileReader(fileName));
System.out.println("-----------------------------------------------------------------------------------------------");
System.out.println("Games Report");
System.out.println("-----------------------------------------------------------------------------------------------");
System.out.println("Gamer 1 2 3 4 5 6 7 8 9 10 Total");
System.out.println("-----------------------------------------------------------------------------------------------");
while ((line = br.readLine()) != null)
{
String[] record = line.split(csvSplitBy);
String[] ints_only = Arrays.copyOfRange(record, 1, record.length);
List<Integer> recordAsInts = Arrays.stream(ints_only)
.map(str -> str.strip())
.map(Integer::parseInt)
.collect(Collectors.toList());
int sum = recordAsInts.stream().reduce(Integer::sum).orElse(0);
System.out.println(record[0] + "\t"
+ record[1] + (record[1].length() > 7 ? "\t" : "\t")
+ record[2] + (record[2].length() > 7 ? "\t" : "\t")
+ record[3] + (record[3].length() > 7 ? "\t" : "\t")
+ record[4] + (record[4].length() > 7 ? "\t" : "\t")
+ record[5] + (record[5].length() > 7 ? "\t" : "\t")
+ record[6] + (record[6].length() > 7 ? "\t" : "\t")
+ record[7] + (record[7].length() > 7 ? "\t" : "\t")
+ record[8] + (record[8].length() > 7 ? "\t" : "\t")
+ record[9] + (record[9].length() > 7 ? "\t" : "\t")
+ record[10] + (record[10].length() > 7 ? "\t" : "\t")
+ sum);
recordCount++;
}
System.out.println("----------------------------------------------------------------------------------------------");
System.out.printf("# of Gamers: %d%n",recordCount);
System.out.println("Top Gamer: ");
System.out.println("----------------------------------------------------------------------------------------------");
br.close();
}
这是我的 output 图像的链接:
这是第 30 行: readWrite(fileName);
这是第 63 行: + record[1] + (record[1].length() > 7? "\t": "\t")
感谢您提供的任何见解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.