【问题标题】:How to use ArrayLists to output (.txt file) a report in a specific format that lists scores of athletes?如何使用 ArrayLists 以特定格式输出(.txt 文件)列出运动员得分的报告?
【发布时间】:2025-11-29 04:30:01
【问题描述】:

我在这个项目上遇到了一些麻烦,非常感谢一些帮助。

这是一个链接: http://www.cse.ohio-state.edu/cse1223/currentsem/projects/CSE1223Project11.html


基本要点是“一个程序,它读取使用特定输入格式的文本文件并使用它生成格式化的输出报告。”

具体来说: “对于本实验,您将编写一个 Java 程序,该程序生成一个简单的格式化报告。该程序将提示用户输入文件名。该文件必须包含特定格式的信息(详情如下)。文件的每个“块”包含一位选手在比赛中的信息——选手的名字后跟该选手所获得的不同分数。程序应该找到每个选手的平均得分、中位得分以及最好和最差得分,并将它们显示在一行最终的总结报告。程序还应该确定哪个球员的平均得分最高,哪个球员的平均得分最低。”


我在尝试编译时遇到以下错误:

Enter an input file name: Project11.txt 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException... -1 
at java.util.ArrayList.elementData(Unknown Source) 
at java.util.ArrayList.get(Unknown Source) 
at Project11.getMedian(Project11.java:68) 
at Project11.main(Project11.java:27) 

很抱歉没有澄清。

第 68 行是:return (inList.get(Middle - 1) + inList.get(Middle)) / 2;
第 27 行是: int median = getMedian(List);

希望对您有所帮助。


这是我的代码:

导入 java.io.; 导入 java.util.;

public class Project11 {

    public static void main(String[] args) throws IOException{
        Scanner in = new Scanner(System.in);
        System.out.print("Enter an input file name: ");
        String input = in.nextLine();
        File inputFile = new File(input);
        List<Integer> List = readNextSeries(inputFile);
        int median = getMedian(List);
        int mean = getAverage(List);
        int max = getMaximum(List);
        int min = getMinimum(List);
        System.out.print("Enter an output file name: ");
        String out = in.nextLine();
        PrintWriter outputFile = new PrintWriter(out);
        System.out.println("Median = " + median);
        System.out.println("Mean = " + mean);
        System.out.println("Max = " + max);
        System.out.println("Min = " + min);
        outputFile.println(median);
        outputFile.println(mean);
        outputFile.println(max);
        outputFile.println(min);
        outputFile.close();
    }  

    // Given a Scanner as input read in a list of integers one at a time     until a negative
    // value is read from the Scanner. Store these integers in an     ArrayList<Integer> and
    // return the ArrayList<Integer> to the calling program.

    private static List<Integer> readNextSeries(File f) {
        ArrayList<Integer> List = new ArrayList<Integer>();
        try {
            Scanner fileScan = new Scanner(f);
            while (fileScan.hasNextInt()) {
                int value = Integer.parseInt(fileScan.next());
                List.add(value);
            }
        } catch (FileNotFoundException e) {}
        return List;
    }

    // Given a List<Integer> of integers, compute the median of the list     and return it to
    // the calling program.

    private static int getMedian(List<Integer> inList) {
        int Middle = inList.size() / 2;
        if (inList.size() % 2 == 1) {
            return inList.get(Middle);
        } 
        else {
            return (inList.get(Middle - 1) + inList.get(Middle)) / 2;
        }
    }

    // Given a List<Integer> of integers, compute the average of the     list and return it to
    // the calling program.

    private static int getAverage(List<Integer> inList) {
        int total = 0;
        int average = 0;
        for(int element:inList){
            total += element;
        }
        average = total / inList.size();
        return average;
    }

    private static int getMaximum(List<Integer> inList) {
        int largest = inList.get(0);
        for (int i = 1; i < inList.size(); i++) {
            if (inList.get(i) > largest) {
                largest = inList.get(i);
            }  
        }
        return largest;
    }

    private static int getMinimum(List<Integer> inList) {
        int smallest = inList.get(0);
        for (int i = 1; i < inList.size(); i++) {
            if (inList.get(i) < smallest) {
                smallest = inList.get(i);
            }
        }
        return smallest;
    }
}

非常感谢您的任何意见。

【问题讨论】:

  • Project11.java:68 - Project11.java:27 不要说“错误是从 68 到 27”.. 它说:“错误是由 main 和更多调用的 getMedian 引起的如果存在..” 发布 Project11 的第 68 行
  • 更好地格式化您的代码并指出哪些行有错误。

标签: java arrays list arraylist


【解决方案1】:

您的 getMedian(...) 方法不处理空列表情况。假设您有一个空列表(大小为 0 的列表),您认为会发生什么?它将达到这一行:

return (inList.get(Middle - 1) + inList.get(Middle)) / 2;

如果列表的大小为 0,则此 inList.get(Middle - 1)inList.get(0 - 1) 相同。您要确保您的方法处理所有不同的情况。我建议为这种特定情况添加一个 if 语句(抛出异常或提供输出,以便用户知道问题所在)。

注意:此“所有案例处理”适用于您的所有方法。

【讨论】: