0
computeHighestMonth(monthlySales)

This method receives the monthly sales array as an argument. This method will search and compare the values of the monthly sales array for the highest value. The method will return the index (or location in the array) of the month with the highest value.

I had it so that it would display the highest sales, but I can't figure out how to include the month name.

public static double computeHighestMonth(double[] monthlySales)
{
    double highestSales = 0;
    for (int index = 0; index < monthlySales.length; index++)
    {
        if (monthlySales[index] > highestSales)
        highestSales = monthlySales[index];
    }
    return highestSales;
}
TryinHard
  • 4,078
  • 3
  • 28
  • 54
Elizabeth
  • 1
  • 1
  • What's the I/P you are giving and what's the O/P you are getting with this code means format. – SatyaTNV Jul 13 '15 at 07:32
  • Not an answer to the question, but since you are talking about sales and use doubles, I suggest looking into `BigDecimals`; `Floating points` are inaccurate. [link1](http://stackoverflow.com/questions/6320209/javawhy-should-we-use-bigdecimal-instead-of-double-in-the-real-world); [link2](http://java-performance.info/bigdecimal-vs-double-in-financial-calculations/). – Kevin Cruijssen Jul 13 '15 at 07:59
  • Did any of the answers help you? if so please accept it. – cнŝdk Jul 15 '15 at 22:28

5 Answers5

0

You should keep not only the highestSales but the index value where it is stored. That means you should declare an integer which should be update everytime

monthlySales[index] > highestSales

is true. This way you would keep which is the "month number", which you should afterwards convert to the string you need.

Hope this helps!

Keews
  • 249
  • 2
  • 15
0

You also need to save the index:

public static double computeHighestMonth(double[] monthlySales)
{
    double highestSales = 0;
    int month = 0;
    for (int index = 0; index < monthlySales.length; index++)
    {
        if (monthlySales[index] > highestSales){
            highestSales = monthlySales[index];
            month = index + 1;
       }
    }
    System.out.print("The salary was highest on: ");
    switch(month){
        case 1: System.out.println("January");
        case 2: System.out.println("February");
        etc.
    }
        return highestSales;
}
moffeltje
  • 4,521
  • 4
  • 33
  • 57
0

Simply instead of returning the highestSales you just need to return the index of this value, and then using this index you can provide the month and the highest value:

public static int computeHighestMonth(double[] monthlySales) {
    double highestIndex = 0;
    for (int index = 0; index < monthlySales.length; index++) {
        if (monthlySales[index] > monthlySales[highestIndex])
        highestIndex = index;
    }
    return highestIndex;
}

And then use the returned value to get the month and its value, like this:

highestIndex = computeHighestMonth(monthlySales);
System.out.println("the highest value is: "+monthlySales[highestIndex]+" of the month "+highestIndex+1);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Would be easier if your monthlySales array would be a List<Double>, because you can then just use

List<Double> monthlySales = ...;
Double highestSales  = Collections.max(monthlySales);
René Link
  • 48,224
  • 13
  • 108
  • 140
0

This will give you the month as a String i.e. January, February etc. Depending on your requirnments instead of you of returning a double you could return a key value pair or a String e.g. month + " had the highest sales " + highestSales

    public static double computeHighestMonth(double[] monthlySales)
    {
        double highestSales = 0;
        String month = "";

        DateFormatSymbols dfs = new DateFormatSymbols();            

        for (int index = 0; index < monthlySales.length; index++)
        {
            if (monthlySales[index] > highestSales) {
                highestSales = monthlySales[index];
                month = dfs.getMonths()[index];
            }
        }

        System.out.println(month);

        return highestSales;
    }
clD
  • 2,523
  • 2
  • 22
  • 38