Advertisement
dimipan80

Exam 3. Exam Score

Sep 10th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.TreeMap;
  3.  
  4. public class _3_ExamScore {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         Scanner scan = new Scanner(System.in);
  9.         scan.nextLine();
  10.         scan.nextLine();
  11.         scan.nextLine();
  12.  
  13.         TreeMap<Integer, TreeMap<String, Double>> examScores = new TreeMap<>();
  14.         String inputLine = scan.nextLine();
  15.         String[] inputs;
  16.         String studentNames;
  17.         int score;
  18.         double grade;
  19.         do {
  20.             inputs = inputLine.split("[^A-Za-z0-9.]+");
  21.             score = Integer.parseInt(inputs[3]);
  22.             studentNames = String.format("%s %s", inputs[1], inputs[2]);
  23.             grade = Double.parseDouble(inputs[4]);
  24.  
  25.             TreeMap<String, Double> studentsMap = examScores.get(score);
  26.             if (!examScores.containsKey(score)) {
  27.                 studentsMap = new TreeMap<>();
  28.             }
  29.  
  30.             studentsMap.put(studentNames, grade);
  31.             examScores.put(score, studentsMap);
  32.  
  33.             inputLine = scan.nextLine();
  34.         } while (!inputLine.contains("--"));
  35.  
  36.         for (int key : examScores.keySet()) {
  37.             System.out.printf("%d -> [", key);
  38.             TreeMap<String, Double> students = examScores.get(key);
  39.             double sumGrades = 0;
  40.             boolean isFirst = true;
  41.             for (String student : students.keySet()) {
  42.                 if (!isFirst) {
  43.                     System.out.print(", ");
  44.                 }
  45.                 isFirst = false;
  46.                 System.out.print(student);
  47.                 sumGrades += students.get(student);
  48.             }
  49.  
  50.             double avg = sumGrades / students.size();
  51.             System.out.printf("]; avg=%.2f%n", avg);
  52.         }
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement