Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
Can you please post code that is SSCCE? –  Uwe Plonus Jun 14 at 13:05
 
and please write variables lowercase: int [] Array => int[] array. –  AlexWien Jun 14 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/… –  Reda Balkouch Jun 14 at 13:06

3 Answers

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.

share|improve this answer

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);
            }
        }
    }
}
share|improve this answer
 
btw, there are more efficient implementations of selection sort. check this out leepoint.net/notes-java/data/arrays/31arrayselectionsort.html –  PrR3 Jun 14 at 13:15

Your problem is on this line

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

it should be

for (int j = 0; j < sizeOfArray; j++)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.