如何获得新创建文件中的月份总数?

时间:2019-05-12 15:05:54

标签: java arrays class sorting methods

我目前正在以此为基础从事C 选项3:下载统计信息

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

ctx

统计文件将具有与输入文件相同的名称,但附加了_stats.txt。例如,如果输入文件名为data.txt,则统计文件将名为data_stats.txt。

我的数据文件包含

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

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

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

我的课:

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

2 个答案:

答案 0 :(得分:1)

在这里,您需要在每个循环中初始化总和。

    Entry tempEntry = null;
    for(Entry entry: uploadResults) {
        float sum;
        sum =entry.getPowerOutput();
    }

float sum;移到总和之前并加到总和上:

    Entry tempEntry = null;
    float sum = 0;
    for(Entry entry: uploadResults) {
        sum += entry.getPowerOutput();
    }

答案 1 :(得分:1)

您可以使用流获取所需结果的地图。

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