Sorting a string array and a int array

I am practising sorting of arrays, and I have successfully sorted a string array.

My little program allows users to enter first number of students, then the name of each one, and at last their grade of each one.

But I also want to sort the int studentGrade array so that the grades in the printout matches the student. Here I am really stuck. See further down for more explanation down in the method: public void sortingAlgorithm

package assignment8exam;

import java.util.Scanner;
import java.util.Arrays;

/**
 *
 * @author Anders
 */
public class Course {

    Scanner sc = new Scanner(System.in);

    public void MainMenu() {

        System.out.println("Enter data about a student, start by entering how many");

        int numbers = sc.nextInt();// amount of student
        String studentNames[] = new String[numbers];
        int studentGrade[] = new int[numbers];
        for (int i = 0; i < numbers; i++) {

            System.out.println("Enter name of student");
            Scanner name = new Scanner(System.in);

            String names = name.nextLine();
            studentNames[i] = names;
        }

        for (int j = 0; j < numbers; j++) {

            System.out.println("Enter grade of student");
            Scanner gradeSc = new Scanner(System.in);

            int grade = gradeSc.nextInt();
            studentGrade[j] = grade;
        }

        sortingArray(studentNames);
        System.out.println("------------------------------------n");
        sortAlgorithm(studentNames, studentGrade);
        System.out.println("What do you want");
        System.out.println("Exit application 1");
        System.out.println("Print out all names of the students 2");
        System.out.println("Print out all the grades of the students 3");
        System.out.println("Print out pairs consisting of “name­grade 4");
        System.out.println("Search for a student - 5");
        Scanner choice = new Scanner(System.in);
        int order = choice.nextInt();

        switch (order) {

            case 1:
                System.exit(1);

            case 2:
                PrintOutNames(numbers, studentNames);

            case 3:
                PrintOutGrades(numbers, studentGrade);

            case 4:
                PrintOutAll(numbers, studentGrade, studentNames);

            case 5:
                search(numbers, studentGrade, studentNames);

        }

    }

    public static void PrintOutNames(int numbers, String studentNames[]) {

        for (int i = 0; i < numbers; i++) {

            System.out.println(studentNames[i]);

        }
    }

    public static void PrintOutGrades(int numbers, int studentGrade[]) {

        for (int i = 0; i < numbers; i++) {

            System.out.println(studentGrade[i]);

        }

    }

    public static void PrintOutAll(int numbers, int studentGrade[], String studentNames[]) {
        System.out.println("--------------------------------------------------------n");
        for (int i = 0; i < numbers; i++) {

            System.out.println("Name----> " + studentNames[i] + " grade ---> " + studentGrade[i]);

        }

    }

    public static void search(int numbers, int studentGrade[], String studentNames[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter name on student you want to search on ");
        String search = sc.nextLine();
        for (int i = 0; i < numbers; i++) {

            if (search.equals(studentNames[i])) {

                System.out.println("Yes we have a student named " + studentNames[i] + " with the Grade " + studentGrade[i] + " n ");

            }

        }
    }

    public static void sortingArray(String studentNames[]) {

        Arrays.sort(studentNames);
        System.out.println("-------------n" + Arrays.toString(studentNames));

    }

    public static void sortAlgorithm(String studentNames[], int studentGrade[]) {

        boolean flag = true;

        while (flag) {

            flag = false;

            for (int j = 0; j < studentNames.length - 1; j++) {

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

                                                                                             // Here i want to place another array that sorts the grade?? how do i do that?
                    }

                }

                System.out.println(Arrays.toString(studentNames));
                System.out.println(Arrays.toString(studentGrade));

            }
        }
    }
}


Source: java

Leave a Reply