例外:java.lang.ArrayIndexOutOfBoundsException

时间:2016-01-06 04:00:56

标签: java arrays for-loop printwriter

我收到此错误:

  

线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:2
  在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;
    }
}

2 个答案:

答案 0 :(得分:0)

代码中的错误是由try块中的此代码引起的:

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

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

此拆分的结果是2个空的String元素,它们对应于之前之后正则表达式之间发生的匹配,这是字符串本身。

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

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

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

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

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

答案 1 :(得分:0)

您的代码存在两个问题:

1)

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

应该是

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

正如您对splitUpReport所做的那样(不确定为什么您实际上会再次分裂)。

2)数组是零索引的,因此print语句应如下所示:

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