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;
}
}