# Finding Min/Max/Average/Sum in an Array.

• 09-16-2013, 02:44 AM
tonythekid92
Finding Min/Max/Average/Sum in an Array.
Pretty new to Java. The error i am getting is that the min value always comes out to be 0 when I dont even enter a 0. I know this is a beginner problem but I just need some help finding the min. What am i doing wrong?
----------------------------

import java.util.Scanner;

public class Arrays {

public static void main(String[] args)
{
int[]numbers=new int[5];
int sum=0;
int max=numbers[0];
int min=numbers[0];
double average=0.0;

Scanner keyboard=new Scanner(System.in);
System.out.println("Enter 5 numbers:");

for(int i=0;i<5;i++)
{
numbers[i]=keyboard.nextInt();
sum=sum+numbers[i];

if (numbers[i] > max)
{
max=numbers[i];
}

else if (numbers[i] < min)
{
min=numbers[i];
}

}

average = sum / 5;

System.out.println("The sum of the numbers is " + sum);
System.out.println("The max number is " + max);
System.out.println("The min number is " + min );
System.out.println("The average of the numbers is " + average);

}
}
• 09-16-2013, 02:50 AM
romero4742
Re: Finding Min/Max/Average/Sum in an Array.
Think about it you initialize an array and all the indexes are set to 0, then you set a variable called min to 0. Every time the user enters something it will be compared to min in this case always 0 therefore the min will always be 0 unless the user enters a negative number.

Cheers
• 09-17-2013, 04:45 AM