我的问题发生在 for 循环将文件中的值读入分数数组期间。该程序读入并打印前 6 个值或 2 行整数,但随后我得到 ArrayIndexOutOfBoundsException: 2
我不知道为什么会停在那里。如果 j<1000,它将读取 17 个值。无论如何,我正在阅读的文件如下(不确定文本文件的格式)。
如有任何帮助,我们将不胜感激
Andy Matt Tom
3 2 3
4 4 5
3 3 2
2 2 2
2 4 2
2 3 2
4 4 5
2 3 3
4 3 5
3 3 6
2 2 5
3 3 3
3 3 2
3 2 4
3 2 6
3 4 3
2 3 2
2 2 2
50 52 62
public static void main( String args[] )
{
try
{
if (args.length<1)
{
System.out.printf("\n...You must enter the name of a file\n");
System.exit(0);
}
Scanner infile = new Scanner ( new File(args[0]) );
int par= 3;
int play= 18;
String[] players= new String[play];
int k=0;
int scores[][]= new int[play-1][par-1];
while(infile.hasNext())
{
players[k]=infile.next();
k++;
if (k==play)
break;
}
for(int j=0; j<par; j++)
{
for (int i=0; i<play; i++)
{
scores[j][i]=infile.nextInt();
System.out.println(scores[j][i]);
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Bug");
System.exit(0);
}
}
最佳答案
int scores[][] = new int [play-1][par-1];
为什么-1
?这就是您的 AIOOB 的来源。
关于java - 将整数读入多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651015/