简体   繁体   English

异常:java.lang.ArrayIndexOutOfBoundsException

[英]Exception : java.lang.ArrayIndexOutOfBoundsException

I am getting this error: 我收到此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:2
at JavaProject.main(JavaProject.java:70) 在JavaProject.main(JavaProject.java:70)

Here is the code: 这是代码:

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!");
}

I believed it to be something related to my for loop, but I have had no luck changing it around. 我认为它与我的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;
    }
}

The error in your code is being caused by this code in the try block: 您的代码中的错误是由try块中的以下代码引起的:

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

You are actually trying to split the gamerReport string using itself as a regex, with a limit of 58 , which is the numerical value of the colon. 您实际上是在尝试使用gamerReport字符串本身作为正则表达式来分割字符串, limit58 ,这是冒号的数值。

The result of this split is 2 empty String elements, which correspond to the match happening before and after the regex, which is the string itself. 拆分的结果是2个空的String元素,它们对应于正则表达式之前之后的匹配,后者是字符串本身。

The ArrayIndexOutOfBounds exception is happening when you try to access the third element from this array: 当您尝试从该数组访问第三个元素时,发生ArrayIndexOutOfBounds异常:

To fix your problem, just define the report array as follows: 要解决您的问题,只需定义report数组,如下所示:

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

As @shmosel pointed out, you might also want to change your array indices here as well: 正如@shmosel指出的那样,您可能还希望在此处更改数组索引:

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

There are two problems with your code: 您的代码有两个问题:

1) 1)

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

should be 应该

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

as you did with splitUpReport (not sure why you're splitting again actually). 就像您对splitUpReport所做的splitUpReport (不确定为什么要再次进行实际拆分)。

2) Arrays are zero-indexed, so the print statement should look like this: 2)数组的索引为零,因此print语句应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 致命异常:主java.lang.ArrayIndexOutOfBoundsException: - FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 异常:java.lang.ArrayIndexOutOfBoundsException OraclePreparedStatement - Exception : java.lang.ArrayIndexOutOfBoundsException OraclePreparedStatement 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM