java - 将整数读入多维数组

标签 java

我的问题发生在 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/

相关文章:

java - 捕获 lambda 表达式的池

java.lang.RuntimeException : java. lang.NullPointerException

java - Android切换按钮点击设置textview

java - 我的 fat jar 与Hadoop中的.jars冲突

java - 无法从对 servlet 的 ajax 调用中检索 session 属性

java - Resteasy:如何保存不固定键的 JSON

java - Spark Kubernetes - 使用 --files 或 spark.files 将配置文件从驱动程序复制到执行程序时出现 FileNotFoundException

java - 结果集编码问题

java - csv 行放入单独的 txt 文件中?

java - Android 网络服务