从arraylist获得价值并找到平均值

时间:2016-02-01 09:29:28

标签: java arraylist

我想弄清楚这个: 我被要求找到温度的总和,但首先我必须将它们放入数组列表中。 到目前为止我的代码:

    public static void main(String[] args) {

    String fileName = "weather.txt";
    File file = new File(fileName);

    Scanner scanner = null;
    try {
        scanner = new Scanner(file); 
    } catch (FileNotFoundException e) {
        System.out.println("The File can't be found!");
        return;
    }

    ArrayList<String> weather = new ArrayList<String>();
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        String[] splittedString = line.split("|");
        String[] split = line.split("\\|");
        System.out.print("{");
        System.out.print("\"Tempreture\" = " + splittedString[0]);
        System.out.print(", \"Humidity\" = " + splittedString[1]);
        String[] date = split[2].split("/");
        System.out.print(", \"Day = " + date[0]);
        System.out.print(", \"Month\" = " + date[1]);
        System.out.print(", \"Year\" = " + date[2]);
        System.out.print("}");

        System.out.println("\n");
        weather.add(splittedString[0]);
        for (int i = 1; i < splittedString.length; i++) {
            System.out.print(splittedString[i]);
        }
        weather.add(splittedString[0]);
        for (int i = 0; i < splittedString.length; i++) {
            System.out.print(splittedString[i]);

        }

我找不到获取温度数据的正确方法,转换为double并找到总和。

来自txt的数据是:

11.4 | 12 | 1995年5月1日

13.0 | 10 | 1995年6月1日

13.5 | 11 | 1995年7月1日

11.2 | 11 | 1995年8月1日

14.6 | 11 | 1995年9月1日

4 个答案:

答案 0 :(得分:1)

您的代码看起来很复杂。你只需做两件事。

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    String[] weatherParameters = line.split("\\|");
    weather.add(weatherParameters[0]); // adds the first split of the string
    // in this case the temperature.
    // weather.add(weatherParameters[1]); // would add the humidity
}

这就是全部。您只需添加splitted String的第一个参数。您只需更改索引即可访问不同的参数。 weatherParameters[2]包含日期。

答案 1 :(得分:1)

如果你的分割是正常的,那么你可以通过使用 sumTemp countTemp 变量来获得平均而不使用 ArrayList ,如下所示:

    double sumTemp = 0;
    int countTemp = 0;
    while() {
     ...............
     ...............

     sumTemp += Double.valueOf(splittedString[0]);
     countTemp++;
     .....................
     .....................
    }//end while

    double avg = sumTemp / countTemp;

答案 2 :(得分:0)

试试这个。

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

        //Split method takes a regular expression and you have to escape the pipe sign.
        weather.add(line.split("\\|")[0]);
}

答案 3 :(得分:0)

您还可以创建一个双精度值列表来保持温度,并对分割方法返回的值使用Double.valueOf(string)

顺便说一下,use the interface when declaring a list是一个很好的做法,在你的情况下是LinkedList might be actually more effective,你可以use type inference during instantiation导致

List<Double> temperatures = new LinkedList<>();
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    String[] weatherParameters = line.split("\\|");
    temperatures.add(Double.valueOf(weatherParameters[0]));
}