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

我目前正在使用此选项3:下载统计信息来处理C语言

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

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,则stats文件将命名为data_stats.txt。

我的数据文件包含

January 10 2018 236.9January 11 2018 267.6January 12 2018 278.1January 13 2018 246.9January 14 2018 262.3January 15 2018 288.6February 
2 2018 199.7February 3 2018 134.6February 4 2018 200.8February 5 2018 198.2March 9 2018 169.7March 10 2018 168.3March 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();
        }

我的课:

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;
    }
一只名叫tom的猫
浏览 510回答 2
2回答

慕村9548890

您可以使用流来获取所需结果的Map。&nbsp;Map<String,&nbsp;Double>&nbsp;collect&nbsp;=&nbsp;uploadResults.stream().collect( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collectors.groupingBy(Entry::getMonth,&nbsp;Collectors.summingDouble(Entry::getPowerOutput)));工作List<Entry>&nbsp;uploadResults&nbsp;=&nbsp;Arrays.asList( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("January","10","2018",236.9f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("January","11","2018",267.6f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("January","12","2018",278.1f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("January","13","2018",246.9f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("January","14","2018",262.3f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("January","15","2018",288.6f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("February","2","2018",199.7f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("February","3","2018",134.6f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("February","4","2018",200.8f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("February","5","2018",198.2f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("March","9","2018",169.7f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("March","10","2018",168.3f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Entry("March","11","2018",179.4f));Map<String,&nbsp;Double>&nbsp;collect&nbsp;=&nbsp;uploadResults.stream().collect( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collectors.groupingBy(Entry::getMonth,&nbsp;Collectors.summingDouble(Entry::getPowerOutput)));System.out.println(collect);产量{March=517.3999938964844,&nbsp;February=733.3000030517578,&nbsp;January=1580.3999938964844}编辑:对于Java8以下的版本,应该这样做Map<String,&nbsp;Float>&nbsp;monthMap&nbsp;=&nbsp;new&nbsp;HashMap<>();for&nbsp;(Entry&nbsp;uploadResult&nbsp;:&nbsp;uploadResults)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;month&nbsp;=&nbsp;uploadResult.getMonth(); &nbsp;&nbsp;&nbsp;&nbsp;Float&nbsp;powerOutput&nbsp;=&nbsp;uploadResult.getPowerOutput(); &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;monthMap.containsKey(month)&nbsp;){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;powerOutput&nbsp;+=&nbsp;monthMap.get(month); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;monthMap.put(&nbsp;month,&nbsp;powerOutput);}System.out.println(monthMap);

繁星点点滴滴

在这里,您可以在每个循环中初始化总和。&nbsp;&nbsp;&nbsp;&nbsp;Entry&nbsp;tempEntry&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;for(Entry&nbsp;entry:&nbsp;uploadResults)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;sum; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;=entry.getPowerOutput(); &nbsp;&nbsp;&nbsp;&nbsp;}float sum;在总和之前移动并添加到总和:&nbsp;&nbsp;&nbsp;&nbsp;Entry&nbsp;tempEntry&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;sum&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;for(Entry&nbsp;entry:&nbsp;uploadResults)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;+=&nbsp;entry.getPowerOutput(); &nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java