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