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 {
// getGrades method used to split String input and find grades
public static int[] getGrades(String input) {
String[] gradesString = input.split("\\D+");
int grades[] = new int[gradesString.length - 1];
for (int i = 0; i < grades.length; i++) {
grades[i] = Integer.parseInt(gradesString[i + 1]);
}
return grades;
}
// 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;
}
// sortGrades method used to sort grades in descending order
public static int[] sortGrades(int grades[]) {
for (int i = 0; i < grades.length; i++) {
int largest = i;
for (int j = largest + 1; j < grades.length; j++) {
if (grades[j] > grades[largest]) {
largest = j;
}
}
int temp = grades[largest];
grades[largest] = grades[i];
grades[i] = temp;
//grades[largest] = temp;
}
return grades;
}
// sortNamesByGrades method used to sort names in grades(descending) order
public static String[] sortNamesByGrades(String names[], int grades[]) {
for (int i = 0; i < grades.length; i++) {
int largest = i;
for (int j = largest + 1; j < grades.length; j++) {
if (grades[j] > grades[largest]) {
largest = j;
}
}
int temp = grades[largest];
grades[largest] = grades[i];
grades[i] = temp;
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;
}
// sortGradesByNames method used to sort grades in name(ascending) order
public static int[] sortGradesByNames(String[] names, int[] grades) {
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
int tempInt = grades[i];
grades[i] = grades[smallest];
grades[smallest] = tempInt;
}
return grades;
}
// putArray method used to display array
public static String putArray(String names[], int grades[],
String heading) {
String output = heading + "\n";
for (int i = 0; i < names.length; i++) {
output += names[i];
int spaces = heading.length();
while (spaces > names[i].length()) {
output += " ";
spaces--;
}
output += grades[i] + "\n";
}
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);
int[] grades = getGrades(input);
String output = "";
output += putArray(names, grades, "Input Order") + "\n";
output += putArray(sortNamesByGrades(names, grades), sortGrades(grades),
"Grade Order:") + "\n";
output += putArray(sortNames(names), sortGradesByNames(names, grades),
"Name Order:") + "\n";
output += "The average grade is " + getAverage(grades) + ".\n"
+ "The highest grade is " + getHighest(grades) + ".\n"
+ "The lowest grades is " + getLowest(grades) + ".";
IO.showMsg(output, "Grades");
System.exit(0);
}
}
Last output of program:
Input Order
Jason 90
Jeff 70
Ann 85
Richard 95
Grade Order:
Richard 95
Jason 90
Ann 85
Jeff 70
Name Order:
Ann 95
Jason 90
Jeff 85
Richard 70
The average grade is 85.0.
The highest grade is 95.
The lowest grades is 70.