Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.TreeMap;
- public class _3_ExamScore {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- scan.nextLine();
- scan.nextLine();
- scan.nextLine();
- TreeMap<Integer, TreeMap<String, Double>> examScores = new TreeMap<>();
- String inputLine = scan.nextLine();
- String[] inputs;
- String studentNames;
- int score;
- double grade;
- do {
- inputs = inputLine.split("[^A-Za-z0-9.]+");
- score = Integer.parseInt(inputs[3]);
- studentNames = String.format("%s %s", inputs[1], inputs[2]);
- grade = Double.parseDouble(inputs[4]);
- TreeMap<String, Double> studentsMap = examScores.get(score);
- if (!examScores.containsKey(score)) {
- studentsMap = new TreeMap<>();
- }
- studentsMap.put(studentNames, grade);
- examScores.put(score, studentsMap);
- inputLine = scan.nextLine();
- } while (!inputLine.contains("--"));
- for (int key : examScores.keySet()) {
- System.out.printf("%d -> [", key);
- TreeMap<String, Double> students = examScores.get(key);
- double sumGrades = 0;
- boolean isFirst = true;
- for (String student : students.keySet()) {
- if (!isFirst) {
- System.out.print(", ");
- }
- isFirst = false;
- System.out.print(student);
- sumGrades += students.get(student);
- }
- double avg = sumGrades / students.size();
- System.out.printf("]; avg=%.2f%n", avg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement