![](/img/trans.png)
[英]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.