0

So the program I'm writing reads a 3 different text files. One text file contains names, the other two contains marks.

Now I have done everything correctly but there is one extra thing I want to add but I have no luck getting it right.

So right now the output file looks like this:

  25987 Alan
  IR101: 35.6      IR102: 20.7      Aggregate: 28.2 

  Class:       Fail       Outcome: Repeat Year!

  -------------------------------------------------------
  25954 Betty
  IR101: 70.2      IR102: 63.4      Aggregate: 66.8 

  Class:       2.1       Outcome: Proceed to Stage 2!

  -------------------------------------------------------
  25654 Chris
  IR101: 58.6      IR102: 35.1      Aggregate: 46.9 

  Class:       Fail       Outcome: Resit IR102!

  -------------------------------------------------------
  Etc

So my program prints out the order in terms of Names from the text file with names. So for example one of the text files that has all the names in it the order is: Alan /n Betty /n Chris

Now I don't want the order to be in names from text file I want the order to be descending aggregate marks. So the order should be:

 25954 Betty
 IR101: 70.2      IR102: 63.4      Aggregate: 66.8 

 Class:       2.1       Outcome: Proceed to Stage 2!

 -------------------------------------------------------

 25654 Chris
 IR101: 58.6      IR102: 35.1      Aggregate: 46.9 

 Class:       Fail       Outcome: Resit IR102!

 -------------------------------------------------------

 25987 Alan
 IR101: 35.6      IR102: 20.7      Aggregate: 28.2 

 Class:       Fail       Outcome: Repeat Year!

 -------------------------------------------------------

I've tried many different solutions for so long but they all fail.

Code for program:

public class SORTING {
static class Student {
    String id;
    String name;
    List<Double> marks;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
        marks = new ArrayList<Double>();
    }


    public void addMark(Double d) {
        marks.add(d);
    }
    public void writeToPW(PrintWriter out) {
        out.println(id + " " + name);
        double d = 0;
        for (int i = 0; i < marks.size(); i++) {
            out.printf("IR10%d: %.1f      ", (i+1), marks.get(i));
            d += marks.get(i);
        }
            out.printf("Aggregate: %.1f ", + d / marks.size());
            out.println("\n");
            double aggregate = (d/marks.size());


            if ((marks.get(0)<40)&&(marks.get(1)>=40)){
            out.print("Class:       Fail" + "       Outcome: Resit IR101");
            }

            if ((marks.get(0)>=40)&&(marks.get(1)<40)){
            out.print("Class:       Fail" + "       Outcome: Resit IR102!");
            }

            if ((marks.get(0)<40)&&(marks.get(1)<40)){
            out.print("Class:       Fail" + "       Outcome: Repeat Year!");
            }

            if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>70)){
            out.print("Class:       1st" + "       Outcome: Proceed to Stage 2!");
            }

            if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=60)&&(aggregate<=69.9)){
            out.print("Class:       2.1" + "       Outcome: Proceed to Stage 2!");
            }
            //2.2 Class degree code.
            if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=50)&&(aggregate<=59.9)){
            out.print("Class:       2.2" + "       Outcome: Proceed to Stage 2!");
            }

            if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=40)&&(aggregate<=49.9)){
            out.print("Class:       3rd" + "       Outcome: Proceed to Stage 2!");
            }
            out.println("\n");
            out.println("-------------------------------------------------------");
        }
    }
public static void main(String[] args) throws IOException {
    //declare reader and writer
    BufferedReader reader = null;
    PrintWriter writer = null;

    //hash maps to store the data
    HashMap<String, Student> students = new HashMap<String, Student>();

    // list to maintain the original order
    List<Student> orderedStudents = new ArrayList<Student>();


    //read the  first file and store the data
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
    String line;
    String[] arg;
    while ((line = reader.readLine()) != null) {
        if (!line.startsWith("-")) {
            arg = line.split(" ");
            Student student = new Student(arg[0], arg[1]);
            students.put(arg[0], student);
            orderedStudents.add(student);
        }
    }
    reader.close();

    //read the second file, merge the data and output the data to the out file
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
    while((line = reader.readLine()) != null){
        arg = line.split(" ");
        students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
    }


    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
    while((line = reader.readLine()) != null){
        arg = line.split(" ");
        students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
    }

    // Now we can do writing.
    writer = new PrintWriter(new FileOutputStream(new File("RankedList.txt")));
    for (Student s: orderedStudents) {
        s.writeToPW(writer);
    }
    writer.close();
}

}

bob9123
  • 725
  • 1
  • 9
  • 31

1 Answers1

1

Well, you need to sort your list, using maybe https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator- . you give it a comparator that is an implementation of the Comparator interface, basically take two Student object and return how they should be ordered, in your case by comparing the aggregate mark. Something like

Collections.sort(orderedStudents, new Comparator<Student>() {
  public int compare(Student s1,Student s2){
    return s2.getAggregate().compareTo(s1.getAggregate());
  }
  });

and expose a getAggregate() method on your Student class that actually calculates the aggregate, instead of having that calculation inside the pretty printing code.

JP Moresmau
  • 7,388
  • 17
  • 31
  • I did something like this were I will create extra methods for Sorting/aggregate but I just can't get it to print to write file – bob9123 Apr 15 '15 at 14:58
  • It currently works with the unsorted list, so it should work the same once the list is sorted. – JP Moresmau Apr 15 '15 at 14:59
  • Okay so from what I'm understanding from your code it's comparing two aggregate scores from students next to each other. And returning the students with the higher score? Am I calling this from the main method writer section? – bob9123 Apr 15 '15 at 15:06
  • Yes, just insert that code before writing, once you've collected all the marks. – JP Moresmau Apr 15 '15 at 15:09
  • How do i fix the issue when the compareTo operator is red/ errors. It says "double cannot be deferenced" – bob9123 Apr 15 '15 at 15:20
  • public double getAggregate(){ double d = 0; double aggregatescore = d/marks.size(); return aggregatescore; } } – bob9123 Apr 15 '15 at 15:21
  • Wrap your double (primitive type) in a Double (class) to get the compareTo function (https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#compareTo-java.lang.Double-) via Double.valueOf(double). Recent version of Java do that for you (autoboxing) – JP Moresmau Apr 15 '15 at 15:22
  • I'm getting confused as we are using both Compare/CompareTo Why is that? has it got something to do with: Collections.sort(orderedStudents, new Comparator() ? I can't get this to work – bob9123 Apr 15 '15 at 15:33
  • compare is what a comparator needs to implement, between two objects. compareTo is what a comparable object can implement. In your case you're using a comparator to compare Student, and to compare their marks we use the fact that Double is comparable (we can compare two double values). – JP Moresmau Apr 15 '15 at 15:47
  • Okay, i finally got that fixed. Now when I run the code it does not actually sort the aggregate marks. – bob9123 Apr 15 '15 at 15:55
  • Do i have to create another writer command so it overwrites the existing one to a new format? – bob9123 Apr 15 '15 at 16:37