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.
Member Avatar for ztini

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];
grades[largest] = grades[i];
grades[i] = temp;

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.

Thanks for both of your advice. I really appreciate it.
I can't use the Student object idea, because we are supposed to be learning certain concepts. But that is the next subject we are going to talk about in class, so I'll keep it in mind.
I did figure out what I was doing wrong, since I was sorting the names first, when I sorted gradesByName there wasn't anything for it to change.
I ended up dumping the methods that only sorted grades and names individualy and changed the sortGradesByName and sortNamesByGrade methods so that they sorted both at the same time.
I obviously have a lot to learn about Java and programming logic.
Again, thanks for your help, I'm sure I'll have more questions in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.