I have been working on this program for the past couple of days and know exactly what I want to do, just not how to go about doing it. Basically I have two arrays, one a string containing student names, the other array an int containing student scores. Both arrays values are inputs entered by the user. Ultimately I am wanting to print out the corresponing names and scores in descending order from highest score to lowest. Now, my problem lies toward the end of the code where I can't for the life of me figure out how to make the two indexes match for println purposes (I have commented where the issue lies. I have the scores printing in desceding order from highest to lowest, but I can't get the names to comply. Any advice on how to solve this issue would be appreciated.
import java.util.Scanner;
public class TestArray
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of students in your class: ");
int number = input.nextInt();
System.out.println("Now enter the " + number + " students names");
String[] nameList = new String[number];
for (int i = 0; i < number; i++) {
nameList[i] = input.next();
}
System.out.println("Next, enter the score of each student: ");
int[] numGrades = new int[number];
for (int i = 0; i < number; i++) {
numGrades[i] = input.nextInt();
}
for (int i = 0; i < number; i++) {
int currentMax = numGrades[i];
int currentMaxIndex = i;
int currentNameIndex = i;
for (int j = i + 1; j < number; j++) {
if (currentMax < numGrades[j]) {
currentMax = numGrades[j];
currentMaxIndex = j; // index max
currentNameIndex = j;
}
}
if (currentMaxIndex != i) {
numGrades[currentMaxIndex] = numGrades[i];
numGrades[i] = currentMax;
}
if (currentNameIndex != i) {
nameList[currentNameIndex] = String.valueOf(nameList[i]);
// need nameList[i] to = the current index of the numGrades array HOW???
}
}
for (int i = 0; i < number; i++) {
System.out.println(nameList[i] + " had a score of " + numGrades[i]);
}
}
}