I'm currently working on C from this Option 3: Download Statistics

If the user chooses this option, the program will create a statistics file with the following data:

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

The statistics file will have the same name as the input file but with _stats.txt appended to it. For example, if the input file was named data.txt, the stats file will be named data_stats.txt.

My data file contains

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

When I run C my total power is 288.6, how do I get the sum of each month?

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

My 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;
    }
}    

2 Answers

1
Community On Best Solutions

You can use streams to get a Map of the required results.

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

Working

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

Output

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

EDIT:

For versions below Java8, this should do

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);
1
Manuel On

Here you initialize your sum in every loop.

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

Move the float sum; before the sum and add to the sum:

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