gpt4 book ai didi

java - 如何使用 split 方法查找 .txt 的 double 的两个特定列的平均值

转载 作者:行者123 更新时间:2023-12-01 21:42:12 25 4
gpt4 key购买 nike

我需要读取此 .txt 文件的行/将它们存储到数组中,同时忽略 T,仅使用 MaxTMinT 列中的数字在本文中找到 MaxT 和 MinT 的平均值,然后将它们打印到 GUI 的标签上。在阅读这些行并按它们分隔的选项卡将它们分开后,我目前陷入困境。

Day MaxT    MinT    AvgT    Dept    HDD CDD Prep    NewSnow
1 61 38 49.5 17.8 15 0 0.00 0.0
2 60 33 46.5 14.7 18 0 0.93 0.0
3 60 36 48.0 16.0 17 0 0.80 0.0
4 36 22 29.0 -3.1 36 0 T T
5 43 18 30.5 -1.8 34 0 0.00 0.0
6 47 22 34.5 2.1 30 0 0.00 0.0
7 53 31 42.0 9.4 23 0 0.00 0.0
8 45 23 34.0 1.2 31 0 0.27 2.1
9 24 15 19.5 -13.5 45 0 0.14 2.0
10 18 11 14.5 -18.7 50 0 0.03 0.7
11 25 11 18.0 -15.4 47 0 T T
12 28 18 23.0 -10.6 42 0 0.01 0.2
13 19 10 14.5 -19.3 50 0 T T
14 21 11 16.0 -18.1 49 0 0.17 2.9
15 36 21 28.5 -5.8 36 0 T 0.0
16 38 25 31.5 -3.1 33 0 0.02 0.0
17 36 26 31.0 -3.8 34 0 T T
18 44 27 35.5 0.4 29 0 0.00 0.0
19 66 39 52.5 17.1 12 0 0.00 0.0
20 72 49 60.5 24.9 4 0 0.00 0.0
21 59 41 50.0 14.1 15 0 1.57 0.0
22 50 33 41.5 5.3 23 0 0.00 0.0
23 56 32 44.0 7.5 21 0 0.02 0.0
24 61 33 47.0 10.2 18 0 1.03 T
25 37 32 34.5 -2.6 30 0 0.04 0.5
26 38 31 34.5 -2.9 30 0 T T
27 52 27 39.5 1.7 25 0 0.00 0.0
28 66 42 54.0 15.9 11 0 T 0.0
29 60 38 49.0 10.8 16 0 0.21 0.0




try{
File inputFile = new File("climateDataCincinnati.txt"); //get text
Scanner in = new Scanner(inputFile); //scanner for text
String line = "";
in.nextLine(); //to consume the first row
while(in.hasNextLine()){ //while there is a next line do
line = in.nextLine(); //take line
String content[] = line.split("\t"); // split by tabs
//where to seperate the two columns from the others
}
catch (IOException exception){
System.out.println(exception.toString());
}

最佳答案

只需将您需要的列存储在列表中即可。当您迭代这些值时,您也可以将字符串解析为 Integer。

List<Integer> maxList = new ArrayList<Integer>();
List<Integer> minList = new ArrayList<Integer>();

while(in.hasNextLine()){ //while there is a next line do
line = in.nextLine(); //take line
String content[] = line.split("\t"); // split by tabs
//get the maxT and minT
Integer maxT = Integer.valueOf(content[1]);
Integer minT = Integer.valueOf(content[2]);
//store for future use
maxList.add(maxT);
minList.add(minT);
}

//Do something on your 2 lists to find the average

关于java - 如何使用 split 方法查找 .txt 的 double 的两个特定列的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36322973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com