This program accepts input in the form of a String of names and grades. It picks out the names and picks out the grades and puts them in seperate arrays. Then it needs to output them together sorted by grades and then by names. I can get the Grade Order to work, but then when I try Name Order it sorts the names, but leaves the grades in grades order.

Any help would be appreciated.

``````// My code so far

import javax.swing.*;
import util.IO;

public class Program7 {

public static int[] getGrades(String input) {

for (int i = 0; i < grades.length; i++) {
}
}

// getNames method used to split Sting input and find names
public static String[] getNames(String input) {
String[] names = input.split("\\d+\\s*");
return names;
}

// getAverage used to find the average of the grades entered
public static double getAverage(int g[]) {
double sum = 0.0;
for (int i = 0; i < g.length; i++) {
sum += g[i];
}
return sum / g.length;
}

// getHighest used to find highest grade entered
public static int getHighest(int g[]) {
int hi = g[0];
for (int i = 1; i < g.length; i++) {
if (g[i] > hi) {
hi = g[i];
}
}
return hi;
}

// getLowest used to find lowest grade entered
public static int getLowest(int g[]) {
int lo = g[0];
for (int i = 1; i < g.length; i++) {
if (g[i] < lo) {
lo = g[i];
}
}
return lo;
}

for (int i = 0; i < grades.length; i++) {
int largest = i;
for (int j = largest + 1; j < grades.length; j++) {
largest = j;
}
}
}
}

for (int i = 0; i < grades.length; i++) {
int largest = i;
for (int j = largest + 1; j < grades.length; j++) {
largest = j;
}
}

String tempString = names[i];
names[i] = names[largest];
names[largest] = tempString;
}
return names;
}

// sortNames method used to sort names in ascending order
public static String[] sortNames(String[] names) {

for (int i = 0; i < names.length - 1; i++) {
int smallest = i;
for (int j = i + 1; j < names.length; j++) {
if (names[j].compareTo(names[smallest]) < 0) {
smallest = j;
}
}
String temp = names[i];
names[i] = names[smallest];
names[smallest] = temp;
}
return names;
}

for (int i = 0; i < names.length - 1; i++) {
int smallest = i;
for (int j = i + 1; j < names.length; j++) {
if (names[j].compareTo(names[smallest]) < 0) {
smallest = j;
}
}
String temp = names[i];
names[i] = names[smallest];
names[smallest] = temp;

// Why doesn't this work
}
}

// putArray method used to display array
public static String putArray(String names[], int grades[],

String output = heading + "\n";
for (int i = 0; i < names.length; i++) {

output += names[i];
while (spaces > names[i].length()) {
output += " ";
spaces--;
}
}
return output;
}

// Main method calls other methods for processing and input/output
public static void main(String[] args) {

String input = JOptionPane.showInputDialog
("Enter one or more names and grades " + "\n"
+ "Example: Jason 90 Jeff 70 Ann 85 Richard 95");
String[] names = getNames(input);

String output = "";

output += putArray(names, grades, "Input Order") + "\n";

"Name Order:") + "\n";

System.exit(0);

}
}
``````

Last output of program:

``````Input Order
Jason      90
Jeff       70
Ann        85
Richard    95

Richard     95
Jason       90
Ann         85
Jeff        70

Name Order:
Ann        95
Jason      90
Jeff       85
Richard    70

``````

## All 3 Replies

The issue you are having is keeping the two arrays in sync with each other. This is why you generally don't want to do it this way. The best approach (that is, the OOP approach) would be to create a Student object with two parameters, grade and name. Then you can perform your array sorts on the student objects using methods like student.getGrade() and student.getName(). Using a student object will eliminate the need to keep 2 arrays in sync; you only need 1 array that you first sort by name and then sort by grade.

On a second note, your getLowest() and getHighest()...if you've sorted your array, you could just return array[0] for getLowest() and array[array.length] for getHighest ... no need for pesky O(n) loops.

@ztini is right.
But if you can't do that for some reason then you can enhance your sort methods to sort both arrays at the same time, eg
in sortGradesByNames you swap elements with

``````int temp = grades[largest];

so then, in the same place, you need to swap the same elements in the other array to keep them in step

``````String temp = names[i];
names[i] = names[smallest];
names[smallest] = temp;``````

but really, make a Student class.