异常:java.lang.ArrayIndexOutOfBoundsException

Exception : java.lang.ArrayIndexOutOfBoundsException

我收到这个错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at JavaProject.main(JavaProject.java:70)

代码如下:

try
{
    PrintWriter writer = new PrintWriter("Gamer Report Data.txt");
    writer.println("Player: " + gamerName);
    writer.println();
    writer.println("-------------------------------");
    String[] report = gamerReport.split(gamerReport, ':');
    writer.println("Game:" + ", score=" + report[1] + ", minutes played=" + report[2] + report[3]);
    writer.close();
} catch (IOException e)
{
    System.err.println("File does not exist!");
}

我认为它与我的 for 循环有关,但我没有运气改变它。

import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;

public class JavaProject {

    private static char[] input;

    @SuppressWarnings("null")
    public static void main(String[] args) {

        for (int b = 1; b < 100; b++) { 
            // this is making the code loop 100 times

            int hrs, mins;
            int[] gameCount;
            int[] minutesPlayed = new int[100];
            String gamerName, gamerReport;

            // Main data storage arrays
            String[] gameNames = new String[100];
            int[] highScores = new int[100];

            Scanner Scan = new Scanner(System.in);

            // formatting for output and input
            System.out.println("////// Game Score Report Generator \\\\\\");
            System.out.println("     ");

            // user enters name and then moves to next line
            System.out.println("Enter Your Name");
            gamerName = Scan.nextLine();

            // user is given an example of input format 
            System.out.println("Input Gamer Information " + "Using Format --> Game : Achievement Score : Minutes Played");
            System.out.println("    ");

            System.out.println("Game : Achievement Score : Minutes Played");
            gamerReport = Scan.nextLine();

            String[] splitUpReport; // an array of string
            splitUpReport = gamerReport.split(":"); // split the text up on the colon

            int i = 0;

            // copy data from split text into main data storage arrays 
            gameNames[i] = splitUpReport[0];
            highScores[i] = Integer.parseInt(splitUpReport[1].trim());
            minutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());

            // output to file 

            try
            {
                PrintWriter writer = new PrintWriter("Gamer Report Data.txt");
                writer.println("Player: " + gamerName);
                writer.println();
                writer.println("-------------------------------");
                String[] report = gamerReport.split(gamerReport, ':');
                writer.println("Game:" + ", score=" + report[1] + ", minutes played=" + report[2] + report[3]);
                writer.close();
            } catch (IOException e)
            {
                System.err.println("File does not exist!");
            }
        }
    }

    public static char[] getInput() {
        return input;
    }

    public static void setInput(char[] input) {
        JavaProject.input = input;
    }
}

您的代码中的错误是由 try 块中的这段代码引起的:

String[] report = gamerReport.split(gamerReport, ':');

您实际上是在尝试使用自身作为正则表达式拆分 gamerReport 字符串,limit58,这是冒号的数值。

这个拆分的结果是 2 个空的 String 元素,对应于 beforeafter 的匹配正则表达式,即字符串本身。

当您尝试访问此数组的第三个元素时发生 ArrayIndexOutOfBounds 异常:

要解决您的问题,只需按如下方式定义 report 数组:

String[] report = gamerReport.split(':');

正如@shmosel 指出的那样,您可能还想在这里更改数组索引:

writer.println("Game:" + ", score=" + report[0] +", minutes played="+ report[1] + report[2]);

您的代码有两个问题:

1)

String[] report = gamerReport.split(gamerReport, ':');

应该是

String[] report = gamerReport.split(":");

就像你对 splitUpReport 所做的那样(不确定你为什么要再次分裂)。

2) 数组是zero-indexed,所以打印语句应该是这样的:

writer.println("Game:" + ", score=" +report[0] +", minutes played="+ report[1] + report[2]);