我知道此错误消息表明我正在尝试通过不存在的索引访问数组中的特定元素,或者它是尝试访问字符串中长度之外的无效元素。但是,我无法根据错误消息中提供的信息确认代码中到底发生了这种情况:
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)
我已经查看了之前发布的有关此问题的问题,但我的代码格式似乎与已发布的其他问题不相似。我试图更改输出的顺序,但由于所需的输出已经打印,所以我停止这样做。我也尝试过换线
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();
}
这是我的输出图像的链接:
这是第 30 行:
readWrite(fileName);
这是第 63 行:
+ record[1] + (record[1].length() > 7 ? "\t" : "\t")