Reputation: 39
I need some guidance on two things my computeLowestMonth method is not working correctly it is just giving me a $0.00 value everytime. Is my for loop wrong?
Second I need to get a better understanding of how to find the lowest value and highest value stored in that particular month then print it out.
static double computeLowestMonth(double[] monthlySales){
double lowest = 0;
for(int i=1; i < monthlySales.length; i++)
{
if (monthlySales[i] < lowest)
lowest = monthlySales[i];
}
System.out.print("Lowest Sales: \t");
System.out.println(f.format(lowest));
return lowest;
}
static void displaySaleInfo(){
}
public static void main(String[] args){
getSales();
totalSales();
computeHighestMonth(monthlySales);
computeLowestMonth(monthlySales);
computeAverageSales(monthlySales);
}//end main
}//end class
Upvotes: 1
Views: 642
Reputation: 36513
You have a couple of problems.
Your loop always skips the first element, because your loops should start with index 0
, not 1
(arrays' indices are zero-based).
You want to initialize your lowest
variable to a value that is larger than any expected value, otherwise, your initial value of 0
might end up being smaller than all of your array's elements.
So you could do something like this:
static double computeLowestMonth(double[] monthlySales){
double lowest = 1000000; // pick some high enough number.
for(int i=0; i < monthlySales.length; i++)
{
if (monthlySales[i] < lowest)
lowest = monthlySales[i];
}
System.out.print("Lowest Sales: \t");
System.out.println(f.format(lowest));
return lowest;
}
Another idea, would be to actually take advantage of your "mistake" of starting to loop on index 1
, and initializing the lowest
variable to the first array element. Like this:
static double computeLowestMonth(double[] monthlySales){
double lowest = monthlySales[0];
for(int i=1; i < monthlySales.length; i++)
{
if (monthlySales[i] < lowest)
lowest = monthlySales[i];
}
System.out.print("Lowest Sales: \t");
System.out.println(f.format(lowest));
return lowest;
}
Or, in Java 8, you can avoid the loop altogether and just do this:
Arrays.stream(monthlySales).min().getAsDouble();
Upvotes: 1
Reputation: 84
you start with
double lowest = 0;
This means that in the following loop, the monthly sales will be compared to the value of $0.00, which would result in NOTHING being lower than $0.00.
You need to start with
double lowest = Double.POSITIVE_INFINITY;
and it should work. One more issue is that you're starting your loop at index = 1, it should be 0 as stated by Benjamin M above.
Upvotes: 2
Reputation: 355
The main issue you are having in computeLowestMonth is that you are starting the minimum at 0, and then expecting values to be lower, which they won't be unless they are negative. While this is messy, you can do:
double lowest = Double.MAX_VALUE;
This will start it off at the highest value, and each lower value will change lowest. You are also doing some other strange things like not catching the return value, and sending static references into the method, but this will fix your problem with it always printing 0.
Upvotes: 0