0

I am new here and I am looking for help with an assignment. My assignment is to take a file with baseball player data (player id, hits, walks, outs) which looks like this (1 5 1 3) inside the file for 20 players and print out a table with column headings (player id, batting average, walks). The batting average is found by diving the players hits by their at bats (hits + outs). I have been working on it for a couple hours and I keep getting an ArrayIndexOutOfBoundsException, which I know means I have exceeded the limits of my array but I don't know how to fix it. I put a try/catch around what I thought was the problem and at least I get some out put starting to look like my table. I posted my work thus far below the output.

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

}

2
  • I posted code under the ouput. Commented Nov 3, 2013 at 1:55
  • @Christian: he does, but it is buried under the exception message. Commented Nov 3, 2013 at 1:55

1 Answer 1

1

The problem

The ArrayOutOfBoundException appears at the line

table[row][column]++;

So either row or column or both exceed the size of the table. Your array tab is initialized like this just before:

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

But rowPlayers is just an array containing the empty string, so its size will be 1.

According to your problem statement, the first column of your input data is the ID of the player, and it is what you read into row. Every time a player has an ID different from 0 (which seems to be all the time), then you have an ArrayOutOfBoundsException.

The same thing applies to the second dimension. I don't know what was your reasoning while writing that algorithm but you don't seem to be going in the right direction there.

Some advice

First, if you don't know the number of players before reading the input, you should use a List instead of an array. If you do know the number of players, initialize the array with that number (but you can still use a list)

Second, consider using an object to represent each row you read from the input file, instead of a table. That is instead of a int[][], you have a List<Row> or a Row[]. Name each field of the object Row with the actual data that you read ("player id", "hits", "walks", and "outs"). This will make things much clearer.

Then you can browse that list of rows and do your computations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.