-2

I'm trying to write a program in Java that takes three arrays and returns the arrays with the lowest value removed. I think I'm creating the new array wrong. While it seems to compile fine, every time I run it, I get the following message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

Any help would be greatly appreciated!

Here is my code:

import java.util.Arrays;

class LowestGrade
{
    public static void main (String [] args)
    {
       int [] a = removeLowest (23, 90, 47, 55, 88);
       int [] b = removeLowest (85, 93, 42);
       int [] c = removeLowest (59, 92, 93, 47, 88, 47);

       System.out.println ("a = " + Arrays.toString(a));
       System.out.println ("b = " + Arrays.toString(b));
       System.out.println ("c = " + Arrays.toString(c));
    }

    public static int[] removeLowest (int...grades)
    {   
       if (grades.length <= 1)
       {
        return grades;
       }  

       else 
       {
          int [] newArray = new int [grades.length - 1];
          int lowest = grades [0];

          for (int i = 0; i < grades.length; i++)
          {
           for (int n = 0; n <= grades.length; n++)
           {
              if (grades[n] > lowest) 
              {
                 newArray[i] = grades[n];
                 i++;
              }

              else 
              {
                 lowest = grades[n];
              }
           }
          }

          return newArray;
       } 
    }
}
srsarkar7
  • 155
  • 5
  • 19

3 Answers3

1

You probably should post the entire exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at LowestGrade.removeLowest(LowestGrade.java:32)
    at LowestGrade.main(LowestGrade.java:7)

This means that you are accessing the 6th element of an array that has less than 6 elements. In your case, you array only has 5 elements, whose valid indexes are 0, 1, 2, 3, and 4.

The fix is to remove the nested loop:

int lowest = grades [0];
for (int i = 1; i < grades.length; i++) 
{
    if(lowest > grades[i]) 
    {
       lowest = grades[i];
    }
}
return removeElement(grades, lowest);

..

public static int[] removeElement(int[] original, int element) 
{ 
   int[] n = new int[original.length - 1]; 
   System.arraycopy(original, 0, n, 0, element ); 
   System.arraycopy(original, element+1, n, element, original.length - element-1); 
   return n; 
}

removeElement from this answer.

Samhain
  • 1,767
  • 1
  • 12
  • 20
Engineer2021
  • 3,288
  • 6
  • 29
  • 51
-1

The following lines are the general programming errors:

  1. newArray[i] = grades[n];

as the newArray size has been reduced by one, it can hold the value when i = grades.length.

  1. for (int n = 0; n <= grades.length; n++)

When n = grades.length, grades[n] throws the ArrayIndexOutOfBoundsException exception. So, should be for (int n = 0; n < grades.length; n++)

In your way, you could simplify and re-write the method as follows:

    public static int[] removeLowest( int... grades ) {

        if ( grades.length <= 1 ) {
            return grades;
        }

        else {

            // find lowest first

            int lowest = grades[ 0 ];

            for ( int i = 1; i < grades.length; i++ ) {

                if ( grades[ i ] < lowest ) {
                    lowest = grades[ i ];
                }
            }

            // build the new array
            int[] newArray = new int[ grades.length - 1 ];

            for ( int i = 0, j = 0; i < grades.length; i++ ) {

                if ( grades[ i ] == lowest ) {
                    continue;
                }

                newArray[ j++ ] = grades[ i ];
            }

            return newArray;
        }
    }
Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
-3

Friendly hint: Check the for-loop bounds of the second for-loop.

Smutje
  • 17,733
  • 4
  • 24
  • 41