0

I have a tutorial in class today with bubble sort and I've got an error I don't know how to fix.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at BubbleSorter.main(BubbleSorter.java:24)

It is not assessed but I would like to get through it to continue on. Thank you. Below is my entire code.

public class BubbleSorter {
    public static void main(String[] args)
    {
        int i;
        int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };
        System.out.println("Array Values before the sort:\n");
        for (i = 0; i < array.length; i++)
            System.out.print(array[i] + " ");
        System.out.println();
        System.out.println();

        bubble_srt(array, array.length);

        System.out.print("Array Values after the sort:\n");
        for (i = 0; i < array.length; i++)
            ;
        System.out.print(array[i] + " ");
        System.out.println();
        System.out.println("PAUSE");
    }

    private static void bubble_srt(int[] array, int length) {
        int i, j, t = 0;
        for (i = 0; i < length; i++) {
            for (j = 1; j < (length - 1); j++) {
                if (array[j - 1] > array[j]) {
                    t = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = t;
                }
            }
        }
    }
}
1
  • 2
    Please format your code. Commented Feb 19, 2013 at 15:13

4 Answers 4

7

You have a small mistake:

This:

for (i = 0; i<array.length; i++);
System.out.print(array[i] + " ");

should be:

//                              v - Semicolon removed
for (i = 0; i<array.length; i++)
    System.out.print(array[i] + " ");
Sign up to request clarification or add additional context in comments.

10 Comments

I imagine this is a formatting issue, otherwise I would have expected a different error message. i.e. symbol i does not exist in Class X
Well, i does exist, it was not declared in the for loop. This is kind of a gotcha I think.
In either case the array out of bounds is coming from the sort itself, not printing the array afterward.
I don't think so, see the method that the exception occurs in: BubbleSorter.main(BubbleSorter.java:24)
i is actually defined at the very top of the method - @Binyamin is correct, this is the cause of the problem
|
2

Also change

for (j = 1; j < (length - 1); j++) {

to

for (j = 1; j < length; j++) {

You left out the last element of the Array!

Output

Array Values before the sort:

12 9 4 99 120 1 3 10 

Array Values after the sort:
1 3 4 9 10 12 99 120 
PAUSE

Comments

0

You need to change 2 lines

for (i = 0; i < array.length; i++)
        ;
System.out.print(array[i] + " ");

to

for (i = 0; i < array.length; i++)
    System.out.print(array[i] + " ");

and

for (j = 1; j < (length - 1); j++) {

to

for (j = 1; j < (length); j++) {

Comments

0

On line 18 you need to get rid of the ; or put in brackets {

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.