gpt4 book ai didi

java - 异常 : java. lang.ArrayIndexOutOfBoundsException

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:25 25 4
gpt4 key购买 nike

我收到此错误:

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;
}
}

最佳答案

您的代码有两个问题:

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]);

关于java - 异常 : java. lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34625414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com