java - 用java制作一个包含数组和文件输入的表格

标签 java arrays exception indexoutofboundsexception

我是新来的,我正在寻求作业方面的帮助。我的任务是获取一个包含棒球运动员数据(球员 ID、安打、保送、出局)的文件,该文件在 20 名球员的文件中看起来像这样 (1 5 1 3),并打印出一个包含列标题的表格(球员 ID、击球数)平均,步行)。击球率是通过将球员的击球数除以击球数(安打+出局)得出的。我已经研究了几个小时,并且不断收到 ArrayIndexOutOfBoundsException,我知道这意味着我已经超出了数组的限制,但我不知道如何修复它。我对我认为的问题进行了尝试/捕获,至少我得到了一些输出,开始看起来像我的 table 。到目前为止,我的工作已发布在输出下方。

Please enter the name of the file containing player statistics.
data
java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException: 6
java.lang.ArrayIndexOutOfBoundsException: 2
java.lang.ArrayIndexOutOfBoundsException: 5
java.lang.ArrayIndexOutOfBoundsException: 3
java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 3
java.lang.ArrayIndexOutOfBoundsException: 5
java.lang.ArrayIndexOutOfBoundsException: 2
java.lang.ArrayIndexOutOfBoundsException: 6
java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 2
java.lang.ArrayIndexOutOfBoundsException: 8
java.lang.ArrayIndexOutOfBoundsException: 3
java.lang.ArrayIndexOutOfBoundsException: 9
java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 10
java.lang.ArrayIndexOutOfBoundsException: 5
java.lang.ArrayIndexOutOfBoundsException: 11
java.lang.ArrayIndexOutOfBoundsException: 6
java.lang.ArrayIndexOutOfBoundsException: 12
java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 13
java.lang.ArrayIndexOutOfBoundsException: 8
java.lang.ArrayIndexOutOfBoundsException: 14
java.lang.ArrayIndexOutOfBoundsException: 9
java.lang.ArrayIndexOutOfBoundsException: 15
java.lang.ArrayIndexOutOfBoundsException: 10
java.lang.ArrayIndexOutOfBoundsException: 16
java.lang.ArrayIndexOutOfBoundsException: 9
java.lang.ArrayIndexOutOfBoundsException: 17
java.lang.ArrayIndexOutOfBoundsException: 8
java.lang.ArrayIndexOutOfBoundsException: 18
java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 19
java.lang.ArrayIndexOutOfBoundsException: 6
java.lang.ArrayIndexOutOfBoundsException: 20
java.lang.ArrayIndexOutOfBoundsException: 4
  Player ID     Bat Avg      Walks
       0           0           0
<小时/>
import java.util.Scanner;
import java.io.*;


public class Lab5 {

int [][] table;
String [] rowPlayers;
String [] colStats;

/**
 * Default constructor
 */
public Lab5() {
    table = new int[0][0];
    rowPlayers = new String[0];
    colStats = new String[0];
}

/**
 * Constructor based on scanner
 * @param rowLabels
 * @param colLabels
 * @param inFile
 */
public Lab5(String[] rowLabels, String[] colLabels, Scanner inFile){
    rowPlayers = rowLabels;
    colStats = colLabels;
    table = new int[rowPlayers.length][colStats.length];
    int row;
    int column;
    while (inFile.hasNext()){
        row = inFile.nextInt();
        column = inFile.nextInt();
        try{
        table[row][column]++;
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }
    }
}

/**
 * Converts the table to a string
 * @return
 */
public String toString() {
    String str = "";

    for (int index = 0; index < colStats.length; index++)
        str = str + "     " + colStats[index];
    str = str + "\n";

    for (int index = 0; index < table.length; index++){
        str = str + rowPlayers[index] + "";
        for (int index2 = 0; index2 < table[index].length; index2++)
            str = str + "           " + table[index][index2];
        str = str + "\n";
    }
    return str;
}

/**
 * Determines each players batting average
 * @return
 */
public String battingAverage(){
    String str = " ";
    float batAvg;

    for (int index = 0; index < rowPlayers.length; index++){
        batAvg = table [index][1] / (table[index][1] + table[index][3]);
    }
    return str;
}

/**
 * Driver
 * Creates a table displaying the players ID number, batting average and walks.
 * @param args
 * @throws java.io.IOException
 */
public static void main (String[] args) throws IOException{
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the name of the file containing player statistics.");
    String fileName = in.nextLine();
    Scanner inFile = new Scanner(new FileReader("C:\\Users\\Ryan\\IdeaProjects\\Lab5\\src\\" + fileName + ".txt" ));
    String [] rowLabels = {""};
    String [] colLabels = {" Player ID", "Bat Avg", " Walks"};

    Lab5 playerStats = new Lab5(rowLabels, colLabels, inFile);
    System.out.println(playerStats);
    inFile.close();
}

}

最佳答案

问题

ArrayOutOfBoundException出现在该行

table[row][column]++;

所以要么 rowcolumn或者两者都超出了表的大小。您的数组tab之前是这样初始化的:

table = new int[rowPlayers.length][colStats.length];

但是 rowPlayers 只是一个包含空字符串的数组,因此它的大小将为 1 .

根据你的问题陈述,你的输入数据的第一列是玩家的ID,它就是你读入row的内容。 。每当玩家的 ID 不为 0 时(似乎一直如此),就会出现 ArrayOutOfBoundsException。

同样的情况也适用于第二个维度。我不知道您在编写该算法时的推理是什么,但您似乎没有朝着正确的方向前进。

一些建议

首先,如果在读取输入之前不知道玩家数量,则应该使用列表而不是数组。如果您确实知道玩家数量,请使用该数字初始化数组(但您仍然可以使用列表)

其次,考虑使用对象而不是表来表示从输入文件中读取的每一行。那不是 int[][] ,你有一个List<Row>Row[] 。使用您读取的实际数据来命名对象 Row 的每个字段(“玩家 ID”、“安打”、“保送”和“出局”)。这将使事情变得更加清晰。

然后您可以浏览该行列表并进行计算。

关于java - 用java制作一个包含数组和文件输入的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19749048/

相关文章:

Java、MyFaces 1.1、Tobago 以及如何创建可重用的 Web 组件

javascript - 收集 div 并对其执行 CSS : HTMLCollection vs Array

c# - 在Silverlight/Windows Phone中进行异常处理的标准方法

java - 为什么方法抛出异常而不是返回 null?

java - dayOfMonth 的值 31 必须在 [1,30] 三月范围内

java - 如何使用 logback 禁用 accessExternalDTD 和 entityExpansionLimit 警告

python - 生成具有随机长度 block 的 1d numpy

C++ 异常处理中的 Else 语句

java - 如何在 java 中使用 truezip 库打开受密码保护的 zip 文件

ios - 对数组对象进行排序以找到最大的数字