How can I sort numbers from highest to lowest and keep names with them? Arrays
I am making a program that asks for how many students and based on that answer a number of boxes come up asking for their name and their score. After it is supposed to sort them from best score to lowest score and keep the name with them. I honestly have no idea what I am doing and need some serious help.
For the purpose of making this code I just hard coded scores and names.
Code java:
String numOfStudents = JOptionPane.showInputDialog("Enter number of students: ");
int numOfStudents1 = Integer.parseInt(numOfStudents);
int[] scores = new int[numOfStudents1];
String [] names = new String[numOfStudents1];
scores[0]= 67;
scores[1]= 98;
scores[2]= 56;
scores[3]= 78;
scores[4]= 100;
names[0] = "Student 1";
names[1] = "Student 2";
names[2] = "Student 3";
names[3] = "Student 4";
names[4] = "Student 5";
sortArray(scores);
System.out.println(names[0] + scores[0]);
System.out.println(names[1] + scores[1]);
System.out.println(names[2] + scores[2]);
System.out.println(names[3] + scores[3]);
System.out.println(names[4] + scores[4]);
}
public static void sortArray(int[] scores) {
for (int i = 0; i < scores.length; i++) {
int lowLoc = i;
int low = scores[i];
for (int x = i; x < scores.length; x++) {
if(low>scores[x]){
low = scores[x];
lowLoc = x;
}//end of if
}//end of inner for
}//end of for
}//end of sortArray
}
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
Java is object-oriented, so my advice would be to take advantage of that feature and create a class which contains the student name and score. You can then create an object/instance of said class containing the appropriate data. You can then sort an array of these and all the information is self-contained within the objects.
See Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
This all has to be in one class. Thanks for the response though!
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
You have an array of scores and an array of names.
You will use the scores to sort them. So what ever happens to the score array should happen to the names array at the same time.
How will you sort the scores to begin with? Get that part working first and worry about letting the names tag along for the ride later on.
Edit: For the record if this was designed to teach arrays, I can think of better demonstrations. This assignment tends to teach bad habits in terms of problem solving. Copeg's suggestion is the real answer to this assignment.
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
Like i said. I cannot use separate classes, they must all be in one. My question is how can I go about sorting them... I don't have a clue how to sort the scores.
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
90 43 98 58 69 20 75 73 40
Can you sort these numbers?
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
I dont think so... because my sortArray method doesnt work how I thought it would...
Re: How can I sort numbers from highest to lowest and keep names with them? Arrays
Quote:
Originally Posted by
ColeTrain
Like i said. I cannot use separate classes, they must all be in one. My question is how can I go about sorting them... I don't have a clue how to sort the scores.
Quote:
Originally Posted by
ColeTrain
I dont think so... because my sortArray method doesnt work how I thought it would...
I meant can you sort the numbers without the program. Just sort them. Think about how you would go about finding the largest or smallest number from all of them. Write down the steps necessary to sort numbers first, and then write code that performs those steps. You know how to sort numbers, and you know how to write code. The problem is you are trying to write code to solve a problem instead of writing code to perform steps that were designed to solve the problem.