I have a project that I need to create 2 Arrays, one to hold Student Names and one to hold Student Scores. The user inputs the size of the array, and the array needs to be sorted using BubbleSort (putting the high scores at the top). I have started the project, created the first array for scores, I have successfully done bubble sort and sorted the grades. Now I can't figure out how to make an array for Names, and once I do how do I make the names array correspond to the Grades array BubbleSort?
Here is the code I have so far.
import java.util.Scanner;
public class Grades {
public static void main(String[]args){
{
Scanner GradeIn = new Scanner(System.in);
Scanner NameIn = new Scanner(System.in);
System.out.print( "How many students are there? " );
int[]GradeArray = new int[GradeIn.nextInt()];
String[]nameArray = new String[GradeIn.nextInt()];
for( int i=0 ; i<GradeArray.length ; i++ )
{
System.out.print( "Enter Grade for Student " + (i+1) + ": " );
GradeArray[i] = GradeIn.nextInt();
System.out.print( "Enter Name of Student " + (i+1) + ": " );
nameArray[i] = NameIn.nextLine();
}
bubbleSort(GradeArray, nameArray);
for( int i : GradeArray ) System.out.println( i );
System.out.println();
}
}
private static void bubbleSort(int[]GradeArray, String[] nameArray){
int n = GradeArray.length;
int temp = 0;
String temp2;
for(int i=0; i<n; i++){
for(int j=1; j<(n-i);j++){
if(GradeArray[j-1]<GradeArray[j]){
//swap
temp=GradeArray[j-1];
GradeArray[j-1]=GradeArray[j];
GradeArray[j]=temp;
temp2=nameArray[j-1];
nameArray[j=1]=nameArray[j];
nameArray[j]=temp2;
}
}
}
}
}
Also how do I change the grades to Double? I started with Int and when I try to change everything to double I get an error saying "Found Double, expected Int".
What the Professor is asking for: Write a program that prompts the user to enter the number of students, the students' names, and their scores, and prints the names in decreasing order according to their scores.
ADDITIONAL INFO:
You will need two arays. One to hold strings. Another to hold the students' scores. (doubles)
The size of the arrays will be entered by the user.
You will have to sort the arrays in the main() method. I recommend using the BubbleSort (http://www.java-examples.com/java-bubble-sort-example) but not as a separate method. HINT: While sorting the grades array, you will need to sort the names array according to the grades.
And finally, you should include a method (void printAnswer(String [] names)) to print out the names array after it has been sorted.