# Math.min not working.

• 02-27-2013, 08:15 PM
Wizard0860
Math.min not working.
User inputs 10 #'s, program tells user sum, average, max, and min. I'll ad average later, but why isnt the min working
Code:

```  import java.util.Scanner;   public class Driver01   {       public static final int NUMITEMS=10;       public static void main (String[] args)       {         double[] array=new double[NUMITEMS];         Scanner keyboard=new Scanner(System.in);         double sum=0;         double average=0;         double min=array[0];         double max=array[0];         for(int x=0;x<NUMITEMS;x++)         {             System.out.print("#"+(x+1)+": ");             array[x] = keyboard.nextDouble();         }         for(int x=0;x<NUMITEMS;x++)         {             sum = array[x]+sum;         }         average =sum/10.0;                 for(int x=0;x<NUMITEMS;x++)         {             min = Math.min(min, array[x]);         }                 for(int x=0;x<NUMITEMS;x++)         {             max=Math.max(array[x],max);         }         System.out.println("Sum: " +sum);         System.out.println("Minimum: " +min);         System.out.println("Maximum: " +max);       }   }```
• 02-27-2013, 08:37 PM
KevinWorkman
Re: Math.min not working.
What do you mean when you say it doesn't work?

What happened when you stepped through this with a debugger, or at least added some print statements, to figure out what's going on?
• 02-27-2013, 08:47 PM
jim829
Re: Math.min not working.
Ok, here are a few observations.

Don't assign min and max until "after" you populate the array. Otherwise, you assign both a default double value which is 0. That is why your program is not working correctly.

Also, a few changes could make it shorter.

1. Assign sum to be equal to array[0] (after the array is filled of course).
2. Then you can use one loop for everything, starting at x = 1.
3. Then calculate average outside the loop
4. You already have a variable named NUMITEMS. So when taking the average, divide by that instead of an explicit 10.

Jim
• 02-27-2013, 08:51 PM
Wizard0860
Re: Math.min not working.
Thanks Jim! It is fixed now.
• 02-27-2013, 09:01 PM
jim829
Re: Math.min not working.
I also noticed that you are calculating average but not printing it out.

Jim
• 02-28-2013, 03:47 AM
Wizard0860
Re: Math.min not working.
Yeah, but as I said in the 1st line of the thread, I'll add it later.