Advertisement
Guest User

ExamScore

a guest
Jul 12th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. package june22;
  2.  
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. import javax.xml.crypto.dsig.keyinfo.KeyValue;
  8.  
  9. public class ExamResults {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner input = new Scanner(System.in);
  13.        
  14.         for (int i = 0; i < 3; i++) {
  15.             input.nextLine();
  16.         }
  17.         TreeMap<Integer, TreeMap<String, Double>> mapmap = new TreeMap<>();
  18.        
  19.         while (true) {
  20.             String nextStudent = input.nextLine();
  21.             if (nextStudent.contains("-")) {
  22.                 break;
  23.             }
  24.             String[] container = nextStudent.split("[^a-zA-Z0-9.]+");
  25.             String name = container[1] + " " + container[2];
  26.             int score = Integer.parseInt(container[3]);
  27.             double mark = Double.parseDouble(container[4]);
  28.            
  29.             double tempValue = 0.0;
  30.            
  31.             if (mapmap.get(score) == null) {
  32.                 mapmap.put(score, new TreeMap<>());
  33.                 mapmap.get(score).put(name, mark);
  34.             }
  35.             else {
  36.                 if (mapmap.get(score).get(name) == null) {
  37.                     mapmap.get(score).put(name, mark);
  38.                 }
  39.                 else {
  40.                     tempValue = mapmap.get(score).get(name) + mark;
  41.                     mapmap.get(score).put(name, tempValue);
  42.                 }
  43.             }
  44.         }
  45.        
  46.         for (Map.Entry<Integer, TreeMap<String, Double>> p : mapmap.entrySet()) {
  47.             System.out.print(p.getKey() + " -> ");
  48.            
  49.             Map<String, Double> temp = p.getValue();
  50.             String output = temp.toString().replaceAll("[(=0-9.0-2+)]", "").replace('{', '[').replace('}', ']');
  51.             System.out.print(output);
  52.             double sum = 0.0;
  53.             int size = 0;
  54.             for (Map.Entry<String, Double> k : temp.entrySet()) {
  55.                 double current = k.getValue();
  56.                 sum += current;
  57.                 size++;
  58.             }
  59.            
  60.             System.out.printf("; avg=%.2f\n", sum / size);
  61.         }
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement