访问List时的IndexOutOfBoundsException

时间:2014-04-10 06:13:52

标签: java arraylist io

我正在为一个班级的项目工作,我想我已经把它弄清楚了,但它一直给我不同的异常错误,现在我很难过。

可在此处找到相关说明:http://www.cse.ohio-state.edu/cse1223/currentsem/projects/CSE1223Project11.html

这是我到目前为止的代码,目前在getMaximum方法中给出了我和IndexOutOfBounds异常。

非常感谢任何帮助。

import java.io.*;
import java.util.*;

public class Project11a {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter an input file name: ");
        String fileName = keyboard.nextLine();
        Scanner in = new Scanner(new File(fileName));
        System.out.print("Enter an output file name: ");
        String outFile = keyboard.nextLine();
        PrintWriter outputFile = new PrintWriter(outFile);
        while (in.hasNextLine()) {
            String name = in.nextLine();
            List<Integer> series = readNextSeries(in);
            int mean = getAverage(series);
            int median = getMedian(series);
            int max = getMaximum(series);
            int min = getMinimum(series);
            outputFile.printf("%-22s%6d%n", name, mean, median, max, min);
        }
        in.close();
        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(Scanner inScanner) {
        List<Integer> nextSeries = new ArrayList<Integer>();
        while (inScanner.hasNextInt()) {
            int currentLine = inScanner.nextInt();
            if (currentLine != -1) {
                nextSeries.add(currentLine);
            } else {
                break;
            }
        }
        return nextSeries;
    }

    // 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) {
        Collections.sort(inList);
        int middle = inList.size() / 2;
        int median = -1;
        if (inList.size() % 2 == 1) {
            median = inList.get(middle);
        } else {
            try {
                median = (inList.get(middle - 1) + inList.get(middle)) / 2;
            } catch (Exception e) {

            }
        }
        return median;
    }

    // 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 average = 0;
        if (inList.size() == 0) {
            return 0;
        }
        for (int i = 0; i < inList.size(); i++) {
            average += inList.get(i);
        }
        return (average / inList.size());
    }

    // Given a List<Integer> of integers, compute the maximum of the list and
    // return it to
    // the calling program.
    private static int getMaximum(List<Integer> inList) {
        int max = inList.get(0);
        for (int i = 1; i < inList.size(); i++) {
            if (inList.get(i) > max) {
                max = inList.get(i);
            }
        }
        return max;
    }

    // Given a List<Integer> of integers, compute the maximum of the list and
    // return it to
    // the calling program.
    private static int getMinimum(List<Integer> inList) {
        int min = inList.get(0);
        for (int i = 1; i < inList.size(); i++) {
            if (inList.get(i) < min) {
                min = inList.get(i);
            }
        }
        return min;
    }

}

1 个答案:

答案 0 :(得分:0)

看来你的清单是空的。 可以触发异常的是声明:

int max = inList.get(0);

因此你的inList在第一个索引中没有值,这意味着inList为空。