使用 FileReader 将数字添加到 ArrayList

Adding numbers to ArrayList with FileReader

我正在尝试从文件中获取数字(作业中指定了 data.txt)并获取这些数字的 minimum/maximum/average。我很难用正在读取的数字填充 ArrayList。

Data.txt这里给出:

16
4
6
5
11
8
17
7
1
10
9
15
12
13
14
2
3

import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
public class DataAnalyzer {

    private String n;

    public DataAnalyzer(String FileName){
        n = FileName;

    }
    public void read(ArrayList<Integer> list) throws IOException{
        File file = new File(n);
        Scanner in = new Scanner(file);

        list = new ArrayList<Integer>();

        while(in.hasNextLine()){
            String line = in.nextLine();

            Scanner scan = new Scanner(line);
            while(scan.hasNextInt()){
                list.add(scan.nextInt());
            }

            scan.close();
        }
        in.close();
    }
    public int getMin(ArrayList<Integer> list){
        int min = 100;
        if (min == 100 && list.size() == 0){
            min = 0;
        }
        for(int i=0; i<list.size(); i++){
            if(list.get(i) < min) min = list.get(i);
        }

        return min;
    }
    public int getMax(ArrayList<Integer> list){
        int max = 0;
        for(int i=0; i<list.size(); i++){
            if(list.get(i) > max) max = list.get(i);
            }
        return max;
    }
    public int getAvg(ArrayList<Integer> list){
        int total = 0;
        int avg = 0;
        if(list.size() == 0){
            avg = 0;
        }
        else{
        for(int i=0; i<list.size(); i++){
            total = list.get(i) + total;
            }
        avg = total/list.size();
        }
        return avg;
    }
}

import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
public class DataAnalyzerTester {
    public static void main(String[] args) throws IOException {
        Scanner console = new Scanner(System.in);

        System.out.println("Please enter a file to analyze: ");
        String FileName = console.next();
        DataAnalyzer test = new DataAnalyzer(FileName);

        ArrayList<Integer> list = new ArrayList<Integer>();
        test.read(list);

        System.out.println("Min: " + test.getMin(list));
        System.out.println("Max: " + test.getMax(list));
        System.out.println("Avg: " + test.getAvg(list));

        console.close();
    }

}

您不能修改调用者的 List 引用。如果一定要 new List,return 就

public List<Integer> read() throws IOException{
    File file = new File(n);
    Scanner in = new Scanner(file);

    List<Integer> list = new ArrayList<Integer>();
    while(in.hasNextLine()){
        String line = in.nextLine();

        Scanner scan = new Scanner(line);
        while(scan.hasNextInt()){
            list.add(scan.nextInt());
        }

        scan.close();
    }
    in.close();
    return list;
}

此外,您可以考虑使用 try-with-resources 来执行那些 close() 调用。而且,我认为你只需要一个 Scanner。喜欢,

public List<Integer> read() throws IOException{
    File file = new File(n);

    List<Integer> list = new ArrayList<Integer>();
    try (Scanner in = new Scanner(file)) {
        while(in.hasNextInt()){
            list.add(in.nextInt());
        }
    }
    return list;
}

最后,您可以传入 list(如果是这样,请不要创建新的本地引用)

public void read(List<Integer> list) throws IOException{
    File file = new File(n);
    try (Scanner in = new Scanner(file)) {
        while(in.hasNextInt()){
            list.add(in.nextInt());
        }
    }
}