0

I am basically trying to sort an input file of Students and Marks into alphabetic and numeric order. I have 4 classes, however I cannot manage to get it to print the student with the mark in any order. Let alone in a alphabetic and numeric order. Any help in how I can get the results printing as a total or any help at all is greatly appreciated. Below is the code I have used for the 4 classes and the input file.

Input File:

Simon 4
Anna 10
Simon 4
Anna 9
Anna 5
Edward 10

Code:

package part1;
import java.io.FileReader;
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        Map<String, StudentMath> map = new HashMap<String, StudentMath>();
        String input = "data/results.txt";
        Scanner scan = new Scanner(new FileReader(input));
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            String[] in = line.split(" ");
            String name = in[0];
            int mark = Integer.parseInt(in[1]);
            //System.out.println(name + ":" + mark);
            StudentMath stud = map.get(name);
            if (stud == null) {
                stud = new StudentMath(name, mark);
                map.put(name, stud);
                stud.sum(mark);
            }

        }
        for (String s: map.keySet()){
            System.out.println(s);
        }
    }
}

package part1;

public class StudentMath extends Main {

    public String name;
    public int mark;

    public StudentMath(String s, int n) {
        name = s;
        mark = n;
    }

    public String getName() {
        return name;

    }
    public int getMark() {
        return mark;
    }

    public int sum() {
       int tot = mark + mark;
       return tot;
    }

    public boolean equals(Object o) {
        if (o instanceof StudentMath) {
            StudentMath m = (StudentMath) o;
            return (name == m.name) && (mark == m.mark);

        }
        else {
            return false;
        }
    }
}

package part1;

import java.util.Comparator;

public class NameCompare implements Comparator<StudentMath> {
    public int compare(StudentMath g1, StudentMath g2) {
        return g1.name.compareTo(g2.name);
    }
}


package part1;


import java.util.Comparator;

public class MarkCompare implements Comparator<StudentMath>{

    public int compare(StudentMath g1, StudentMath g2) {
        return g2.mark - g1.mark;
    }
}
1
  • It is not clear what exactly are you having problems with Commented Mar 17, 2012 at 20:23

2 Answers 2

2

A HashMap is an unordered collection, so it does not define an ordering over it's keys. That is why you get your output in random order.

You could try using an ordered collection type, such as a List (ArrayList or LinkedList), or if you really need to have a Map then you could look at the TreeMap class. TreeMap is a Map implementation that knows how to sort its keys. Note that TreeMap is not able to sort by value, so it probably isn't the right data structure to use here because you won't be able to (easily) use it to sort by mark.

I won't give you code because this is clearly homework, but hopefully that will get you on the right track.

EDIT: To address the comment below: ordered is not the same as sorted. To actually sort a List into the order you want, take a look at the Collections.sort methods.

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

6 Comments

A list is not an ordered collection.Unless you are refering to the insertion order
@user384706: Um...yes it is. The behaviour of the Iterator obtained from a list is well defined: the list is always in some known order and (unless the underlying data changes) the iterator will always return items in the same order. A HashMap does not provide this guarantee, hence it is "unordered". A sorted collection is always an ordered collection, but an ordered collection is not necessarily sorted.
The thing is, I need to send the mark to the StudentMath class, calculate the total, and return it to the Main class. However, I cannot get this to work effectively.
You've got the right idea by looking for the student's name and creating a new StudentMath object if it's not found. Now think about what happens when you look up the map and it doesn't return null. What does your code do? What should it do?
@CameronSkinner:the list is always in some known order and the iterator returns items in the same order.Yes but this is the insertion order not the natural order.From your comment and answer I am still not clear on what order you are talking about
|
1

Never ever compare Strings using "==" always use "equals" to compare. in the line

return (name == m.name) && (mark == m.mark);

change it to

return (name.equals(m.name))&& (mark == m.mark);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.