-3

I'm new to Java, this is my first week and I don't have any homework to do, but I want to practice the language. I need to get the 5 largest numbers from an array, and store them in another one. After searching in the forum, I found the Arrays.sort() method and I'm really surprised about Java. But my code isn't right. I can't find out why... Could you please help me?

package week1;
import java.util.Arrays;

public class Week1 {
    public static void main(String[] args) {
        int maxValues[] = new int [5];
        int array[] = new int [50];
        for (int i = 0; i <array.length; i++) {
            int x = (int) ( Math.random() * 100 );
            array[i]=x;
            System.out.println("The Number is: " + array[i]);
        }
       Arrays.sort(array);
       for (int i = 0; i < maxValues.length; i++) {
           for (int j =array.length; j > (array.length)-5; j--) {
               maxValues[i]=array[j];
           }
           System.out.println( (i+1) +"Max is: " + maxValues[i] );
        }
    }
}
Haris Papadakis
  • 69
  • 2
  • 12
  • 1
    Why not use the debugger built in to Netbeans? – President James K. Polk Mar 10 '17 at 22:23
  • your problem is within the second for loop. – Ousmane D. Mar 10 '17 at 22:25
  • The exception says: ArrayIndexOutOfBoundsException: 50, and it happens on line 16 of Week1.java. That should allow you to find the problem by yourself. 50 is the value of the index that is out of bounds. – JB Nizet Mar 10 '17 at 22:27
  • here is the solution: After you do `Arrays.sort(array);` simply your largest 5 numbers are at end of the array >>, in this case, you can apply `for(int i=a.length-1;i>a.length-5;--i){maxValues[4-i]=array[i];}` –  Mar 10 '17 at 22:29

2 Answers2

0

Change int j =array.length to int j =array.length-1. If you start with array[50] you'll get the ArrayIndexOutOfBoundsException because the highest index in this array is 49 (array.length-1)

Jérôme
  • 1,133
  • 2
  • 16
  • 21
0

You don't need two for loops to find the max values and store them. You know where the greatest number is inside your int array, array.length - 1. The next highest is array.length - 2, array.length - 3 and so on and so forth. Just need to be sure that when you decrement that the index is >= 0 to avoid the array index out of bounds exception. Your previous code attempted to access array.length as an index but because arrays are 0 based that index doesn't exist and that's why you get an array out of bounds exception when you attempt to find the highest number at index 50 but the last index array of that size is 49.

    int maxValues[] = new int [5];
    int array[] = new int [50];
    for (int i = 0; i <array.length; i++) {
        int x = (int) ( Math.random() * 100 );
        array[i]=x;
        System.out.println("The Number is: " + array[i]);
    }
   Arrays.sort(array);
   int index = array.length - 1;
   for (int i = 0; i < maxValues.length && index >= 0; i++) {
           maxValues[i]=array[index];
           index--;
       System.out.println( (i+1) +"Max is: " + maxValues[i] );
    }
RAZ_Muh_Taz
  • 3,964
  • 1
  • 10
  • 22