Java help. Array sorting.

Associate
Joined
22 May 2004
Posts
1,792
Location
N.Ireland
Hi guys could anyone help me. I am writing this program in net beans for a night class and I have got the MArks to sort by ID but when I try to add anyother loop to get them to sort by ID and Marks it doesnt work Im not sure what im doing wrong.

Any chance anyone could add the loop so the ID's and the Marks sort. It only needs an extra loop for the marks.

Code:
/*
 *Class Name:Pc2
 *Project Name:Pc2
 *Package Name. pkgpc2
 *Created on 24 January 2008, 09:50
 *Version Number. 1.0
 *Author: Gareth McGibbon
 *Description:Program to sort out Syudent data for maths and english
 *Also has a menu option for useers to interact with. 
 */


        

package pkgpc2;
import keyb.InOut;

public class pkgpc2
{
    //================================================== ======== //
    // NAME : Array Data
    // RETURN TYPE : N/A
    // PARAMETERS : mathMark: Math students results
    //              engMark :English Students results
    //              mathId :Maths Student ID 
    //              engId :English Student ID
    // DESCRIPTION : The maths and english studentId and Marks. 
    // This is done as four arrays.
    //
    // //================================================== ========
    static void initialiseArrays(int[] mathMark, int[] engMark, String[] mathsId, String[] engId)
    {
        //Math Mark. Sample Data
        mathMark[0] = 72;
        mathMark[1] = 84;
        mathMark[2] = 21;
        mathMark[3] = 48;
        mathMark[4] = 55;
        mathMark[5] = 49;
        mathMark[6] = 78;
        mathMark[7] = 21;
        mathMark[8] = 35;
        mathMark[9] = 98;
        mathMark[10] = 44;
        mathMark[11] = 56;
        
        //Math ID. Sample Data
        mathsId[0] = "M1005";
        mathsId[1] = "M1002";
        mathsId[2] = "M1000";
        mathsId[3] = "M1003";
        mathsId[4] = "M1001";
        mathsId[5] = "M1004";
        mathsId[6] = "M1006";
        mathsId[7] = "M1010";
        mathsId[8] = "M1009";
        mathsId[9] = "M1008";
        mathsId[10] = "M1007";
        mathsId[11] = "M1011";
        
        
        //English Mark. Sample Data
        engMark[0] = 82;
        engMark[1] = 56;
        engMark[2] = 34;
        engMark[3] = 42;
        engMark[4] = 71;
        engMark[5] = 55;
        engMark[6] = 67;
        engMark[7] = 34;
        engMark[8] = 28;
        engMark[9] = 59;
        engMark[10] = 47;
        engMark[11] = 65;
        engMark[12] = 52;
        engMark[13] = 12;
        engMark[14] = 74;

        

        //English ID. Sample Data
        engId[0] = "E2003";
        engId[1] = "E2005";
        engId[2] = "E2001";
        engId[3] = "E2002";
        engId[4] = "E2006";
        engId[5] = "E2000";
        engId[6] = "E2004";
        engId[7] = "E2008";
        engId[8] = "E2007";
        engId[9] = "E2010";
        engId[10] = "E2009";
        engId[11] = "E2012";
        engId[12] = "E2011";
        engId[13] = "E2013";
        engId[14] = "E2014";
        
    } 

    //================================================== ========
    //
    // NAME : Menu
    // RETURN TYPE : none
    // PARAMETERS : none
    // DESCRIPTION : Menu for user to pick options          
    // 
    //
    // //================================================== ========
    static void menu()
    {
        System.out.println("\n\n\tXXXXXXXXXXXXXXXXXXXXXXXX XXXXX");
        System.out.println("\tX STUDENT RECORDS MAIN MENU X");
        System.out.println("\tXXXXXXXXXXXXXXXXXXXXXXXXXXXX X");
        System.out.println("\n\n1. - All Mathematics students");
        System.out.println("\n2. - All English students sorted by Student Id");
        System.out.println("\n3. - display English Merit students");
        System.out.println("\n4. - display the average examination marks");
        System.out.println("\n5. - All students above the Mathematics average");
        System.out.println("\n6. - EXIT");
    } 

    //================================================== ========
    //
    // NAME : displayStudents
    // RETURN TYPE : none
    // PARAMETERS : studentId 
    //      studentMark : Test score
    //      size : Array length
    //      upperLimitMark : upper inclusion mark in display
    //      lowerLimitMark : lower inclusion mark in display
    //      category : displays the category of display
    // DESCRIPTION : displays all students by Id alongside
    // their corresponding exam marks, as well as
    // identifying the group being displayed.
    //
    // //================================================== ========
    static void displayStudents(String[] studentId, int[] studentMark, int size, int upperMark, int lowerMark, String category)
    {
        //Command for system to print out title
        System.out.println("\n " + category + "\n");
        System.out.println("Student Identity\t Student");
        System.out.println(" Number\t\tExam Mark\n");

        //Start of loop
        for (int count = 0; count <= size - 1; count++)
        {
            if (studentMark[count] >= lowerMark && studentMark[count] < upperMark)
            {
                System.out.println(" " + studentId[count] + "\t\t " + studentMark[count]);
            } 

        } //End of loop

    } 

    //================================================== ========
    //
    // NAME : sortStudents
    // RETURN TYPE : none
    // PARAMETERS : studentId : maths student's  Id 
    // studentMark : maths student's test score
    // size : size of the array
    // DESCRIPTION : Sorts students in ascending order of student Id
    //
    // //================================================== ========
    static void sortStudents(String[] studentId, int[] studentMark, int size)
    {
       //Start Loop
        for (int count = 0; count < size - 2; count++)
        {
            
            for (int index = (count + 1); index < size - 1; index++)
            {
                //Use if statement to compare, and swap student Ids if required
                if (studentId[count].compareTo(studentId[index]) > 0)
                {
                    String tempId = studentId[count];
                    studentId[count] = studentId[index];
                    studentId[index] = tempId;
                } 
            } 
        } //End of loop
    } 

    //================================================== ======== //
    // NAME : averageResult
    // RETURN TYPE : double
    // PARAMETERS : studentId : maths student's unique Id 
    // studentMark: maths student's test score
    // size : size of the array
    // dESCRIPTION : Displays average student marks. 
    //
    // //================================================== ========
    static double averageResult(int[] studentMark, int size)
    {
        //declare variables
        double totalMark = 0, averageMark = 0;

        //Start of loop
        for (int count = 0; count <= size - 1; count++)
        {
            totalMark = studentMark[count] + totalMark;
        } //End of loop
        averageMark = totalMark / size;
        return averageMark;
    } 

    //================================================== ========
    // NAME : main
    // RETURN TYPE : none
    // PARAMETERS : args : [DEFAULT]
    // DESCRIPTION : Menu so user can pick what function they want 
    // program to proform.
    //================================================== ========
    public static void main(String[] args)
    {
        //Declare array
        int[] mathExamMark = new int[12];
        int[] engExamMark = new int[15];
        String[] mathStudentId = new String[12];
        String[] engStudentId = new String[15];

        //Initialise Array
        initialiseArrays(mathExamMark, engExamMark, mathStudentId, engStudentId);

        //Declare variables
        int menuOption = 0;
        boolean menuExit = false;
        int mathArraySize = mathStudentId.length;
        int englishArraySize = engStudentId.length;

        while (menuExit != true)
        {
            menu();

            
            System.out.println("\n\nPlease select a menu option number to continue.");
            menuOption = InOut.readInt();

           
            while (menuOption < 1 || menuOption > 6)
            {
                System.out.println("\n\n++++++++++++++++++++++++++ +++++++++++++++++++++++++++++");
                System.out.println("+ INVALID! - selection should be in the range 1 - 6! +");
                System.out.println("++++++++++++++++++++++++++++++ +++++++++++++++++++++++++\n");
                menu();
                System.out.println("\nPlease select a menu option number to continue.");
                menuOption = InOut.readInt();
            } 

            switch (menuOption)
            {
                    case 1:
                    displayStudents(mathStudentId, mathExamMark, mathArraySize, 100, 1, "All Maths students\n");
                    break;
                    case 2:
                    sortStudents(engStudentId, engExamMark, englishArraySize);
                    displayStudents(engStudentId, engExamMark, englishArraySize, 100, 1, "English students in Ascending order\n");
                    break;
                    case 3:
                    displayStudents(engStudentId, engExamMark, englishArraySize, 100, 55, "English Merit students\n ");
                    break;
                    case 4:
                    averageResult(mathExamMark, mathArraySize);
                    System.out.println("\nThe Maths average is " + averageResult(mathExamMark, mathArraySize));
                    averageResult(engExamMark, englishArraySize);
                    System.out.println("\nThe English average is " + averageResult(engExamMark, englishArraySize));
                    break;
                    case 5:
                    averageResult(mathExamMark, mathArraySize);
                    displayStudents(mathStudentId, mathExamMark, mathArraySize, 100,(int) averageResult(mathExamMark, mathArraySize) + 1, "Mathematics students above the average\n **************************************");
                    break;
               
                    case 6:
                    menuExit = true;
                    System.exit(0);
            } 
        } 
    }//End of main() method
}

Any help would be appricated
 
Lol thats just cofused me even more.

Here is the test data Im using.

Maths Examination

Student ID Mark
M1005"; 72
"M1002"; 84
"M1000"; 21
"M1003"; 48
"M1001"; 55
"M1004"; 49
"M1006"; 78
"M1010"; 21
"M1009"; 35
"M1008"; 98
"M1007"; 44
"M1011"; 56

English Exam
Student ID Mark
"E2003"; 82
"E2005"; 56
"E2001"; 34
"E2002"; 42
"E2006"; 71
"E2000"; 55
"E2004"; 67
"E2008"; 34
"E2007"; 28
"E2010"; 59
"E2009"; 47
"E2012"; 65
"E2011"; 52
"E2013"; 12
"E2014"; 74

So I need the program to link these results and put them in ascending by the student ID. So it would be

E2001 34
E2002 42

And so on.
 
Thanks for your help mate but it seems they are looking me to do it using loops. Im not sure. So confused at the moment and its needs to be handed in tomorrow. And its the only part im stuck on.

Anyone else have any idea's?

Guys if anyone can sort this out and post the working code Ill drop you some paypal for a few drinks when I get paid on the 29th of this month. Getting desperate at the moment. Need to hand it in tomorrow and dont want to have to quit the course over this stupid thing as as soon as we have handed this in thats the java part done.

Gareth
 
Last edited:
Nah I want the IDs and Marks to correspond and then the whole lot be sorted by the ID so it will output...

E2001 34
E2002 42
E2003 82

And so on. THe id's in order with the correct marks corresponding with them. I only need this working for the english marks.

That does make sense doesnt it.

At the moment the program is just sorting the ID's and the corresponing marks arnt beside them in the output window.
 
For a night class in Java this code seems to be a very strange way of teaching you Java. You nearly never have to use code like this in the real world because there are much better ways of doing this, I did do array sorting at Uni when we learnt Java but we were taught about all of the sorting algorithms such as selection sort, insertion sort, bubble sort, quick sort and merge sort.

It looks to me like you need to sort one array whilst also using the same sorting order to sort the second array so that they will match up. In the real world a collection of objects would be used to do this which makes it so much easier.

That could be true. Sitting here completly lost at the moment. And if i dont have the code working by 4.30 I will fail so no point going to class feel like throwing in the towel. :(

Anyone fancy booting up netbeans and trying toi get this stupid thing working?
 
I just realised you are already doing a bubble sort, what exactly is the problem here? If you need to sort the marks along side the students, you're practically there, I'll add the bit to you're code to demonstrate (it's not the way I'd have done it, but you don't seem to be allowed to use collections etc, also you don't really need to pass "size" but I'll keep it there to make it easier to understand):

Code:
    static void sortStudents(String[] studentId, int[] studentMark, int size)
    {
       //Start Loop
        for (int count = 0; count < size - 2; count++)
        {
            
            for (int index = (count + 1); index < size - 1; index++)
            {
                //Use if statement to compare, and swap student Ids if required
                if (studentId[count].compareTo(studentId[index]) > 0)
                {
                    String tempId = studentId[count];
                    studentId[count] = studentId[index];
                    studentId[index] = tempId;

                    // ADD THIS BIT TO SORT THE MARKS
                    int tempMark = studentMark[count];
                    studentMark[count] = studentMark[index];
                    studentMark[index] = tempMark;
                } 
            } 
        } //End of loop
    }

This would be so much easier using a more object oriented style of programming, really all you should need to do is use a collection such as a TreeMap<String, Integer> which would map student IDs to marks and auto sort them.

God I love you. I could kiss you right now. :D Thank you very much thank you thank you thank you.

You have saved mny skin.

Mate will you drop me your paypal address and ill drop you money for a few pints when I get paid on the 29th of this month as the way of thanks

Gareth
 
Back
Top Bottom