0

私はここに来たばかりで、課題について助けを求めています。私の課題は、野球選手のデータ (選手 ID、安打、四球、アウト) を含むファイルを 20 選手のファイル内で (1 5 1 3) のように取得し、列見出し (選手 ID、バッティング) を含む表を出力することです。平均、歩く)。打率は、打席数 (安打 + アウト) で打席数を割ることによって求められます。私は数時間それに取り組んできましたが、ArrayIndexOutOfBoundsException を取得し続けています。これは、配列の制限を超えたことを意味しますが、修正方法がわかりません。私は問題だと思ったところに try/catch を配置し、少なくとも私のテーブルのように見えるようになりました。これまでの作品を出力の下に投稿しました。

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

}

4

1 に答える 1

1

問題

が行にArrayOutOfBoundException表示されます

table[row][column]++;

そのため、どちらrowcolumnまたは両方がテーブルのサイズを超えています。配列tabは直前に次のように初期化されます。

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

しかし、rowPlayers は空の文字列を含む単なる配列なので、そのサイズは になります1

問題の説明によると、入力データの最初の列はプレーヤーの ID であり、それを に読み込んだものですrow。プレーヤーが 0 以外の ID を持つたびに (これは常にそうです)、ArrayOutOfBoundsException が発生します。

同じことが 2 次元にも当てはまります。そのアルゴリズムを書いているときのあなたの推論が何であったかはわかりませんが、あなたはそこで正しい方向に進んでいないようです.

いくつかのアドバイス

まず、入力を読み取る前にプレーヤーの数がわからない場合は、配列の代わりにリストを使用する必要があります。プレーヤーの数がわかっている場合は、その数で配列を初期化します (ただし、リストを使用することはできます)。

次に、テーブルではなく、オブジェクトを使用して、入力ファイルから読み取った各行を表すことを検討してください。つまり、 の代わりにint[][]List<Row>または がありRow[]ます。オブジェクト Row の各フィールドに、読み取った実際のデータ ("player id"、"hits"、"walks"、および "outs") を指定します。これにより、物事がより明確になります。

次に、その行のリストを参照して、計算を行うことができます。

于 2013-11-03T02:05:42.923 に答える