1

I have been assigned a task that requires me to utilise a 2D Array. I have to read in a file and export it to a 2D array. I can only have a single method but I am unable to sort the array correctly. I am supposed to sort the data in 3 ways (alphabetically by name and with scores highest to lowest; highest to lowest scores for each student and highest to lowest by the average of 3 scores.) So far I have

import java.util.*;
import java.io.*;

public class ScoreSorter {
    public static void main(String[] args) {

        int student_num = 30;
        String[][] DataInTableArr = new String[30][6];
        try {
            BufferedReader ReadIn = new BufferedReader(new FileReader("classZ.csv"));
            for (int i = 0; i < 30; i++) {
                String DataIn = ReadIn.readLine();
                String[] DataInArr = DataIn.split(",");
                DataInTableArr[i][0] = DataInArr[0];
                DataInTableArr[i][1] = DataInArr[1];
                DataInTableArr[i][2] = DataInArr[2];
                DataInTableArr[i][3] = DataInArr[3];
                int temptest1 = Integer.parseInt(DataInArr[1]);
                int temptest2 = Integer.parseInt(DataInArr[2]);
                int temptest3 = Integer.parseInt(DataInArr[3]);
            }
        } catch (Exception e) {
            System.out.println("Whoops, you messed up, RESTART THE PROGRAM!!!!!");
        }
    }
}

I have no idea as to how to solve the rest of the task... I would appreciate if someone could tell me of the most efficient way and perhaps an example...

1
  • 2
    No, we will not delete this post. You have a history of removing your posts once you've received an answer, and this runs completely counter to the spirit of Stack Overflow. If you want to request that this account be disassociated from your post, please contact the community team with your request: stackoverflow.com/contact Commented Nov 15, 2015 at 21:08

2 Answers 2

2

One plausible way is to create a Student class which implements Comparable interface, with the following members:

String name;
int scoreOne;
int scoreTwo;
int scoreThree;
compareTo(Student s) { //implement the comparison following 3 criteria you mentioned }

And, read the files row by row, for each row we create a Student object, and put all rows in a TreeSet. In this way, the TreeSet together with the compareTo method will help us sort the Students automatically.

Finally, iterate the sorted TreeSet to fill up the 2D array.

Sign up to request clarification or add additional context in comments.

1 Comment

I believe the method proposed by @Andreas is better than mine - we can do it without using a TreeSet. So please continue your work based on that method (as shown in your PasetBin codes) :-)
1
import java.util.*;
import java.io.*;

public class ScoreSorter {
public static void main(String[] args) {

    int student_num = 30;
    String[][] DataInTableArr = new String[30][6];
    try {
        BufferedReader ReadIn = new BufferedReader(new FileReader("classZ.csv"));
        for (int i = 0; i < 30; i++) {
            String DataIn = ReadIn.readLine();
            String[] DataInArr = DataIn.split(",");
            DataInTableArr[i][0] = DataInArr[0];
            DataInTableArr[i][1] = DataInArr[1];
            DataInTableArr[i][2] = DataInArr[2];
            DataInTableArr[i][3] = DataInArr[3];
            int temptest1 = Integer.parseInt(DataInArr[1]);
            int temptest2 = Integer.parseInt(DataInArr[2]);
            int temptest3 = Integer.parseInt(DataInArr[3]);
        }
  /*Code To be Inserted Here*/
    } catch (Exception e) {
        System.out.println("Whoops, you messed up, RESTART THE PROGRAM!!!!!");
    }
}
}

If there are 6 columns such that First is name and the other 3 are scores then what does other 2 columns contain?? I ignore your array declaration :

String[][] DataInTableArr = new String[30][6];

and assume it to be 30x4 array

String[][] DataInTableArr = new String[30][4];

Logic for sorting Alphabetically

if(DataInTableArr[i][0].compareTo(DataInTableArr[i+1][0])){
     /* Sorting Name of adjacent rows*/
     String temp = DataInTableArr[i][0];
     DataInTableArr[i][0] = DataInTableArr[i+1][0];
     DataInTableArr[i+1][0] = temp;

     /*Sorting the three marks similarly*/

     temp = DataInTableArr[i][1];
     DataInTableArr[i][1] = DataInTableArr[i+1][1];
     DataInTableArr[i+1][1] = temp;

     temp = DataInTableArr[i][2];
     DataInTableArr[i][2] = DataInTableArr[i+1][2];
     DataInTableArr[i+1][2] = temp;

     temp = DataInTableArr[i][3];
     DataInTableArr[i][3] = DataInTableArr[i+1][3];
     DataInTableArr[i+1][3] = temp;

}

Put the above code in bubble sorting algorithm i.e. 2 loops.

Logic for sorting according to highest marks

In this case you have to find the highest marks in all three subjects of each DataInTableArr[i] and then compare the highest marks with that of next row.

Logic for sorting according to Average marks

Calculate the average of each i'th row as

(Integer.parseInt(DataInTableArr[i][1]) + Integer.parseInt(DataInTableArr[i][2]) + Integer.parseInt(DataInTableArr[i][3]))/3

and compare it with [i+1] th rows average.(same formula just replace [i] with [i+1])

2 Comments

There are many sorting algorithms, but bubblesort is one of the worst, performance-wise. Just use the Java built-in sorting. --- Why swap individual values when you can just swap the row arrays (String[] row = DataInTableArr[i]; DataInTableArr[i] = DataInTableArr[i+1]; DataInTableArr[i+1] = row;)?
Oh right we can swap complete row. Thanks for the remark @Andreas . I recommended bubble sorting because it is easy for a beginner to implement it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.