Results 1 to 6 of 6
Thread: Sort Student Names and Grades
- 10-23-2011, 08:24 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Sort Student Names and Grades
Write a program that prompts the user to enter the number of students, the students' names, and their scores, and prints student names in decreasing order of their scores.
Here is my code:
My code compiles however I have no idea how to get the student names to show up in decreasing order by score. How do I do this?Java Code:import java.util.*; public class SortingStudents { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of students: "); int numofstudents = input.nextInt(); int[] array = new int[numofstudents]; for(int i = 0; i < numofstudents; i++) { System.out.print("Enter the student's name: "); String name = input.next(); System.out.print("Enter the student's score: "); double score = input.nextDouble(); } selectionSort(array); } public static void selectionSort(int[] array) { for(int i = array.length - 1; i >= 1; i--) { int 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) { array[currentMaxIndex] = array[i]; array[i] = currentMax; } } } System.out.println(array); } }
- 10-23-2011, 09:22 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Sort Student Names and Grades
In the for loop of main() you say
But you don't actually do anything with name or score. Effectively they are forgotten about as the next iteration of the for loop begins.Java Code:System.out.print("Enter the student's name: "); String name = input.next(); System.out.print("Enter the student's score: "); double score = input.nextDouble();
Clearly you have to do something with these values that the user has entered. Like put them into an array (or two). Once you have this done (and can print the student names in the order they were entered) you can move on to actually sorting them.
-----
Have you learnt how to define your own classes? I ask because it makes more sense to create a Student class that represents the student (both name and score). If you haven't that's no problem, but you will probably have to create two arrays: one for the students' names and one for their scores.
- 10-23-2011, 10:00 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Re: Sort Student Names and Grades
Ok thanks. I changed those four lines of code and put name and score into arrays.
Here is my code now:
To print out the student's name in order of which they were entered I would use this:Java Code:import java.util.*; public class SortingStudents { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of students: "); int numofstudents = input.nextInt(); String[] names = new String[numofstudents]; int[] array = new int[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] = input.nextInt(); } selectionSort(array); } public static void selectionSort(int[] array) { for(int i = array.length - 1; i >= 1; i--) { int 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) { array[currentMaxIndex] = array[i]; array[i] = currentMax; } System.out.println(array[i]); } } } }
How would I actually sort them? I'm having difficulty with this. No, I have not learned how to define my own classes. I'm taking an intro level java class and we haven't gotten to that yet.Java Code:System.out.println(Arrays.toString(names));
- 10-24-2011, 12:06 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Sort Student Names and Grades
What makes sorting the names difficult is the fact that you are sorting them based on the scores.How would I actually sort them? I'm having difficulty with this.
First make sure that selectionSort() is working correctly. Ie if you say in main():
you should see the scores in numeric order. If not there's some problem in the sort method.Java Code:selectionSort(array); System.out.println(Arrays.toString(array));
Then you have to change the sort method to take account of the fact that you are sorting one thing in terms of another. The obvious change is that you need to pass both arrays to the sort method:
I've called them keyArr and valueArr but you would call this method likeJava Code:/** Sorts both array arguments so that the second is in order. */ public static void selectionSort(String[] keyArr, int[] valueArr) { // changed code here }
The changes to the sort code are fairly straightforward:Java Code:selectionSort(names, array); // now print pnames
* Whenever you have a comparison (ie the conditions of the if statements) you use the value array (the one with the scores).
* But every time to alter array values (ie the assignment statements inside the second if statement) you alter both arrays in the same way so that names stay in the same position in their array as their associated scores in the other array.
- 10-24-2011, 03:29 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Re: Sort Student Names and Grades
Thank you. I was finally able to solve the problem thanks to your help. Here is the final code:
Java Code:import java.util.*; public class SortingStudents { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of students: "); int numofstudents = input.nextInt(); String[] names = new String[numofstudents]; int[] array = new int[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] = input.nextInt(); } selectionSort(names, array); System.out.println(Arrays.toString(names)); } public static void selectionSort(String[] names, int[] array) { for(int i = array.length - 1; i >= 1; i--) { String temp; int 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; } } } }
- 10-24-2011, 04:04 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
Passing Grades Help!
By JojoDiaz in forum New To JavaReplies: 16Last Post: 10-14-2011, 03:48 PM -
Got myself in a bind trying to sort names with GUI entry
By Veronicad1 in forum New To JavaReplies: 26Last Post: 05-13-2011, 03:54 PM -
Student Name Bubble Sort, Read And Write From Text Files
By vanek in forum New To JavaReplies: 18Last Post: 05-02-2011, 05:37 PM -
array to sort names in alphabetical order
By leoshiner in forum New To JavaReplies: 6Last Post: 05-01-2011, 12:28 PM -
Array help counting # of grades
By speaknspell in forum New To JavaReplies: 4Last Post: 04-16-2009, 10:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks