使用FileReader向ArrayList添加数字

时间:2015-01-22 03:03:33

标签: java arraylist filereader

我试图从文件中取数字(在分配中指定data.txt)并取这些数字的最小值/最大值/平均值。我很难用正在读取的数字填充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();
    }

}

1 个答案:

答案 0 :(得分:0)

您无法修改来电者的List参考。如果您必须new List,请将其退回

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