[英]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
字符串本身作为正则表达式来分割字符串, limit
为58
,这是冒号的数值。
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.