gpt4 book ai didi

java - 如何获取新创建的文件中的月份总数?

转载 作者:行者123 更新时间:2023-12-02 01:30:46 24 4
gpt4 key购买 nike

我目前正在研究C语言选项 3:下载统计数据

如果用户选择此选项,程序将创建一个包含以下数据的统计文件:

a.  Power output sorted from lowest to highest
b. Day with highest output
c. Total by month
d. Average power output for all the data

统计文件将与输入文件同名,但附加 _stats.txt。例如,如果输入文件名为 data.txt,则统计文件将命名为 data_stats.txt。

我的数据文件包含

January 10 2018 236.9
January 11 2018 267.6
January 12 2018 278.1
January 13 2018 246.9
January 14 2018 262.3
January 15 2018 288.6
February 2 2018 199.7
February 3 2018 134.6
February 4 2018 200.8
February 5 2018 198.2
March 9 2018 169.7
March 10 2018 168.3
March 11 2018 179.4

当我运行C时,我的总功率是288.6,我如何获得每个月的总和?

 public static void createStatsFile(ArrayList<Entry> uploadResults) {
///////////////
///////A///////
///////////////
try {
PrintWriter writer = new PrintWriter("data_stat.txt", "UTF-8");

ArrayList<Entry> lowestToHighest = bubbleSort(uploadResults);

for(Entry entry : lowestToHighest) {
writer.print(
"Month: " + entry.getMonth() + " Day: " + entry.getDay() + " Year: " + entry.getYear() + " Power Output: " + entry.getPowerOutput());
writer.print("\n");
entry.print();
}

///////////////
///////B///////
///////////////
Entry tmpEntry = null;

for(Entry entry : uploadResults) {
if(tmpEntry == null) {
tmpEntry = entry;
} else {
if(entry.getPowerOutput() > tmpEntry.getPowerOutput()) {
tmpEntry = entry;
}
}
}

writer.write("Day of Max Power Outage: " + tmpEntry.getMonth() + " " + tmpEntry.getPowerOutput());

///////////////
///////C///////
///////////////

//ArrayList<Entry> totalByMonth = bubbleSort(uploadResults);
Entry tempEntry = null;
for(Entry entry: uploadResults) {
float sum;
sum =entry.getPowerOutput();
}

writer.write("Total power output by month: " + tmpEntry.getMonthList() + " " + tmpEntry.getPowerOutput());
writer.close();

我的类(class):

public class Entry {
//declaring variables
private String month;
private String day;
private String year;
private float powerOutput;
private String file;
private int[] MonthList;
String inputFile = file;

//Constructors
public Entry() {
}

public Entry(String m, String d, String y, float p) {
month = m;
day = d;
year = y;
powerOutput = p;
}

//creating print to call ArrayList in main
public void print() {
System.out.println("Month: " + month + " Day: " + day + " Year: " + year + " Power Output: " + powerOutput);
}

public static void getFile() {

}

public String getMonth() {
return month;
}

public String getDay() {
return day;
}

public String getYear() {
return year;
}

public float getPowerOutput() {
return powerOutput;
}

public int[] getMonthList() {
return MonthList;
}

public String getInputFile() {
return inputFile;
}
}

最佳答案

您可以使用流来获取所需结果的 Map。

 Map<String, Double> collect = uploadResults.stream().collect(
Collectors.groupingBy(Entry::getMonth, Collectors.summingDouble(Entry::getPowerOutput)));

工作中

List<Entry> uploadResults = Arrays.asList(
new Entry("January","10","2018",236.9f),
new Entry("January","11","2018",267.6f),
new Entry("January","12","2018",278.1f),
new Entry("January","13","2018",246.9f),
new Entry("January","14","2018",262.3f),
new Entry("January","15","2018",288.6f),
new Entry("February","2","2018",199.7f),
new Entry("February","3","2018",134.6f),
new Entry("February","4","2018",200.8f),
new Entry("February","5","2018",198.2f),
new Entry("March","9","2018",169.7f),
new Entry("March","10","2018",168.3f),
new Entry("March","11","2018",179.4f)
);

Map<String, Double> collect = uploadResults.stream().collect(
Collectors.groupingBy(Entry::getMonth, Collectors.summingDouble(Entry::getPowerOutput)));

System.out.println(collect);

输出

{March=517.3999938964844, February=733.3000030517578, January=1580.3999938964844}

编辑:

对于 Java8 以下的版本,应该这样做

Map<String, Float> monthMap = new HashMap<>();
for (Entry uploadResult : uploadResults) {
String month = uploadResult.getMonth();
Float powerOutput = uploadResult.getPowerOutput();

if( monthMap.containsKey(month) ){
powerOutput += monthMap.get(month);
}
monthMap.put( month, powerOutput);
}
System.out.println(monthMap);

关于java - 如何获取新创建的文件中的月份总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100521/

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