# Arrays and Loops Problem

1. Sep 27, 2011

### major_maths

I'm having issues with a sorting class that I'm trying to call from my main program, TestStudent. I don't think it's taking the user's input correctly when I try to read it into an array but I can't figure out why.

class Student:
Code (Text):
public static void selectionSort(double[] numbers)
{
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;
}
}
TestStudent:
Code (Text):
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);

double[] number = new double[10];
do{
switch (letter3)
{
case 'y':
System.out.println("Great! Please enter the averages to be sorted.");
for(int m=0; m<number.length; m++)
{number[m] = keyboard.nextDouble();}

Student.selectionSort(number);

for(int l=0; l<number.length; l++)
{System.out.println(number[l]+" ");}

System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
response4 = keyboard.next();
break;
case 'Y':
System.out.println("Cool! Please enter the averages to be sorted.");
for(int m=0; m<number.length; m++)
{number[m] = keyboard.nextDouble();}

Student.selectionSort(number);

for(int l=0; l<number.length; l++)
{System.out.println(number[l]+" ");}

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:
}
}while(response3.equalsIgnoreCase("y")&&response4.equalsIgnoreCase("y"));

2. Sep 27, 2011

### Staff: Mentor

Your code is more complicated than it needs to be, with duplicate cases for 'Y' and 'y' and with two response variables.

You can simplify your switch statement like so:
Code (Text):

switch (letter3)
{
case 'y':
case 'Y':
System.out.println("Cool! Please enter the averages to be sorted.");
for(int m=0; m<number.length; m++)
{number[m] = keyboard.nextDouble();}

Student.selectionSort(number);

for(int l=0; l<number.length; l++)
{System.out.println(number[l]+" ");}

System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
response4 = keyboard.next();
break;
default: