954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array Out of Bounds Exception

I'm having trouble debugging one of my methods. The class Student inherits a class called Person and the methods are called in the main program, TestStudent. I'm trying to create a method that lets the user sort an array of grade averages in ascending order but whenever I try, I get a run-time error that says
"java.lang.ArrayIndexOutOfBoundsException: 1"

Here's the Student class method:

public static String selectionSort()
    {        
        double[] numbers = {0};
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Great! Please enter the averages to be sorted.");
        for(int k=0; k<numbers.length; k++)
        {numbers[k] = keyboard.nextDouble();}
        
        int min_index;
        double temp;
        for(int i=0; i<numbers.length-1; i++)
        {
            min_index = i;
            for(int j=min_index+1; j<numbers.length; j++)
            {
                if (numbers[j]<numbers[min_index])
                {min_index=j;}
            }
            temp = numbers[min_index];
            numbers[min_index] = numbers[i];
            numbers[i] = temp;
        }
        return ("The averages were sorted as follows: "+numbers[numbers.length]);
    }


And here's how I call the method in TestStudent:

Scanner keyboard = new Scanner(System.in);
 System.out.println("Would you like to sort students' scores according to average?");
       String response3 = keyboard.next();
       char letter3 = response3.charAt(0);
       String response4 = "n";
       char letter4 = response4.charAt(0);       
       do{
           switch (letter3)
           {
               case 'y':
                    System.out.println(Student.selectionSort());
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response3 = keyboard.next();
                    break;
                case 'Y':
                    System.out.println(Student.selectionSort());
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response4 = keyboard.next();
                    break;
                case 'n':
                    break;
                case 'N':
                    break;
                default:
                    System.out.println("Could not recognize answer.");
            }
        }while(response3.equalsIgnoreCase("y")&&response4.equalsIgnoreCase("y"));


Please let me know if you would like to see either program in full.

murkycrimson
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

The error message also tells you the exact line where the error occurred. That would be very useful to know.
But in the meantime you create an array numbers with exactly one element (length 1), then make lots of uses of it which seem to imply that it's longer than that?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Watch your array (number).You haven't set the size of array.
Correct that !!!!

ajaxx195
Newbie Poster
4 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

@ajaxx195
Actually the code sets the size of array to 1.

double[] numbers = {0};  // means numbers is now [0]

The problem is that using array with [] is a static size array. The array size cannot be changed during the program unless you redeclare it... In other words, once it passes the first element, it will go out of bound...

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 
Watch your array (number).You haven't set the size of array.


Do you mean numbers? if so double[] numbers = {0};
declares and initialises it to one element (value 0.0) - so the size is 1.

snap!

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: