-3

Im writing a program to get three numbers from user and put it into an array list and find the sum, avg, min, and max. Im having problems with finding the min. When i run the code the Min is always zero. How do i fix this problem?

    int num = 0;
    int sum = 0;
    int avg = 0;
    int min = 0;
    int max = 0;
    int i = 0;

    ArrayList<Integer> array = new ArrayList<Integer> ();

    Scanner input = new Scanner (System.in);

    for (i = 0; i < 3; ++i){

        System.out.println("Enter a number");
        num = input.nextInt();
        array.add(num);

        sum += num;

        if(num > max){
            max = num;
        }

        if(num < min){
            min = num;
        }

    }

    avg = sum / i;

    System.out.println("Sum: " + sum);
    System.out.println("Avg: " + avg);
    System.out.println("Max: " + max);
    System.out.println("Min: " + min);



 //Code Result

 //Enter a number
 // 10
 //Enter a number
 // 20
 //Enter a number
 // 30

 //Sum: 60
 //Avg: 20
 //Max: 30
 //Min: 0 <-- this is always zero

 }
Sean
  • 37
  • 1
  • 6
  • 2
    instead of `int min = 0;` use `int min = Integer.MAX_VALUE;` – Blackbelt Oct 30 '16 at 07:40
  • 2
    There was a similar question with the same problem literally minutes before yours [here](http://stackoverflow.com/questions/40326803/java-assignment-find-min-sum-of-odd-integers-and-count-of-negative-integers). – Turing85 Oct 30 '16 at 07:42
  • (The point of the exercise may be something else entirely - think _Stream_.)(Blackbelt's comment analogously applies to `max`.) – greybeard Oct 30 '16 at 08:35

2 Answers2

3

Good morning, Sean.

You have to initialize 'min' value outside the loop, and you must initialize 'min' to large value, something like 'Integer.MAX_VALUE'.

Anyway, I will tell you how you can find some elements without a loop:

To get the max value of an ArrayList you don't need a for. You can use Collections API to find it.

Collections.max(arrayList);

It returns you the max value of all in the ArrayList.

To get the min value you have too a function in Collections API to find it.

Collections.min(arrayList);

will work too.

Sum and Avg you must calculate with a loop, and it's ok in your programm.

Your code must be something like this

int num = 0;
int sum = 0;
int avg = 0;
int min = 0;
int max = 0;
int i = 0;

ArrayList<Integer> array = new ArrayList<Integer> ();

Scanner input = new Scanner (System.in);

for (i = 0; i < 3; ++i){

    System.out.println("Enter a number");
    num = input.nextInt();
    array.add(num);

    sum += num;
}

max = Collections.max(array);
min = Collections.min(array);
avg = sum / i;

System.out.println("Sum: " + sum);
System.out.println("Avg: " + avg);
System.out.println("Max: " + max);
System.out.println("Min: " + min);

PD: You must import java.util.Collections;

Have a good day!

1

that's because you set min to be 0 and it is lower than all values. you need to initialize it to a large number (say Integer.MAX_VALUE) then it will work

Turing85
  • 18,217
  • 7
  • 33
  • 58
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47