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.