0

How do I fix this error and what does it mean?

java.lang.ArrayIndexOutOfBoundsException: 5
    at Sort.sort(Sort.java:29)
    at Sort.<init>(Sort.java:13)
    at SortedArray.<init>(SortedArray.java:23)

Here is the code:

import java.util.Scanner;
import java.util.Random;

public class SortedArray
{
    Scanner input = new Scanner(System.in);
    int [] Array;
    Sort sortedArray;
    int sizeOfArray;

    public SortedArray()
    {
        System.out.print("Enter the number of values to put in the array: ");
        sizeOfArray = input.nextInt();
        Array = new int [sizeOfArray];
        System.out.println("");
        for(int i = 0; i < sizeOfArray; i++)
        {
            Random r = new Random();
            Array[i] = r.nextInt(100) + 1;
            System.out.println(Array[i]);
        }
        sortedArray = new Sort(Array, sizeOfArray);
        sortedArray.display();
    } 
}


public class Sort
{
  int[] array;
  int sizeOfArray;
  public Sort(int[] oldArray, int sizeOfOldArray)
  {
    sizeOfArray = sizeOfOldArray;
    array = new int [sizeOfArray];
    for( int i = 0; i < sizeOfArray; i++)
    {
        array[i] = oldArray[i];
    }
    sort();
  }

  public void display()
    { 
       for ( int i = 0; i < sizeOfArray; i++){
           System.out.println(array[i]);
       }
    }

  private void sort()
    {
        for (int i = 0; i < sizeOfArray; i++)
        {
            for (int j = 0; j < sizeOfArray; i++)
            {
                if (array[j] < array[i])
                {
                    swap(i,j);
                }
            }
        }
    }

    private void swap(int x, int y)
    {
        int temp;
        temp = array[x];
        array[x] = array[y];
        array[y] = temp;
  }
}

I get the error when I run the program and enter the value. The program is supposed to sort the numbers from greatest to least. I'm not sure what is wrong.

3
  • Can you please post code that is SSCCE? Commented Jun 14, 2013 at 13:05
  • and please write variables lowercase: int [] Array => int[] array. Commented Jun 14, 2013 at 13:06
  • You can sort your array with Arrays.sort(int[] a) method. have a look here : docs.oracle.com/javase/6/docs/api/java/util/… Commented Jun 14, 2013 at 13:06

3 Answers 3

3

First, what it means: you have an array and are trying to acces an index that is outside its range (below 0 or bigger or equal than the length of the array).

The probable cause is:

for (int j = 0; j < sizeOfArray; i++)

Notice that you check that j does not get too big but you are increasing i.

Sign up to request clarification or add additional context in comments.

Comments

1

The Problem is that the inner loop also incrementsi which isn't correct in this situation!

private void sort()
{
    for (int i = 0; i < sizeOfArray; i++)
    {
        for (int j = 0; j < sizeOfArray; j++)
        {
            if (array[j] < array[i])
            {
                swap(i,j);
            }
        }
    }
}

1 Comment

btw, there are more efficient implementations of selection sort. check this out leepoint.net/notes-java/data/arrays/31arrayselectionsort.html
1

Your problem is on this line

for (int j = 0; j < sizeOfArray; i++)

it should be

for (int j = 0; j < sizeOfArray; j++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.