我是新来的,我正在寻找作业方面的帮助。我的任务是在 20 名球员的文件中获取一个包含棒球运动员数据(球员 id、命中、步行、出局)的文件(1 5 1 3),并打印出一个带有列标题的表格(球员 id、击球平均,步行)。击球率是通过击球手击球(击球+出局)来确定击球率的。我已经为此工作了几个小时,并且不断收到 ArrayIndexOutOfBoundsException,我知道这意味着我已经超出了数组的限制,但我不知道如何修复它。我尝试/抓住了我认为是问题的地方,至少我得到了一些开始看起来像我的桌子的东西。到目前为止,我将我的工作发布在输出下方。
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();
}
}