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.

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?

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

@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...

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.