I have to make a program where you ask user to enter any number of students, that asks the name and grade of each student. So if I said 2 students, I would put billy smith, then 54, then it would ask me the name of the 2nd student, john smith, then the grade, 81. Then it outputs the names with grades in descending order of grades. It would output:
name---------grades
------------------
John smith 81
billy smith 54
I have evrything except for it printing it out. I need it to print out the name with the grade. Here is what I have:
import java.util.*;
public class assignment5 {
public static void main(String[] args) {
// Scanner for first name and last name with space in between.
java.util.Scanner input = new java.util.Scanner(System.in);
input.useDelimiter(System.getProperty("line.separator"));
System.out.print("Enter the number of students: ");
int numofstudents = input.nextInt();
String[] names = new String[numofstudents];
Double[] array = new Double[numofstudents];
for(int i = 0; i < numofstudents; i++) {
System.out.print("Enter the student's name: ");
names[i] =input.next();
System.out.print("Enter the student's score: ");
array[i] = (Double) input.nextDouble();
}
System.out.print("Name" + "\tScore");
System.out.print("\n----" + "\t----\n");
selectionSort(names, array);
System.out.println(Arrays.toString(names));
}
public static void selectionSort(String[] names, Double[] array) {
for(int i = array.length - 1; i >= 1; i--) {
String temp;
Double currentMax = array[0];
int currentMaxIndex = 0;
for(int j = 1; j <= i; j++) {
if (currentMax > array[j]) {
currentMax = array[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i) {
temp = names[currentMaxIndex];
names[currentMaxIndex] = names[i];
names[i] = temp;
array[currentMaxIndex] = array[i];
array[i] = currentMax;
}
}
}
}