Advertisement
nikiii07

Exam Score Solution

Nov 13th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  * Created by snap2n on 13-Nov-15.
  5.  */
  6. public class ExamScore {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         ArrayList<String> tableLines = new ArrayList<>();
  11.         while (sc.hasNextLine()){
  12.             String inputLine = sc.nextLine();
  13.             if (inputLine.isEmpty()) {
  14.                 break;
  15.             }
  16.             tableLines.add(inputLine);
  17.         }
  18.         TreeMap<Integer, TreeMap<String, Double>> wholeMap = new TreeMap<>();
  19.         ArrayList<String[]> tokens = remakeInput(tableLines);
  20.  
  21.         for (String[] token : tokens) {
  22.             StringBuilder currentStudentSB = new StringBuilder();
  23.             currentStudentSB.append(token[0]);
  24.             currentStudentSB.append(" ");
  25.             currentStudentSB.append(token[1]);
  26.             String currentStudent = currentStudentSB.toString();
  27.             int currentExamScore = Integer.parseInt(token[2]);
  28.             double currentGrade = Double.parseDouble(token[3]);
  29.  
  30.             if (!wholeMap.containsKey(currentExamScore)){
  31.                 wholeMap.put(currentExamScore, new TreeMap<>());
  32.                 wholeMap.get(currentExamScore).put(currentStudent, currentGrade);
  33.             }else{
  34.                 if (!wholeMap.get(currentExamScore).containsKey(currentStudent)){
  35.                     wholeMap.get(currentExamScore).put(currentStudent, currentGrade);
  36.                 }
  37.             }
  38.         }
  39.  
  40.         for (Map.Entry<Integer, TreeMap<String, Double>> score: wholeMap.entrySet()) {
  41.             System.out.printf("%d -> ", score.getKey());
  42.             ArrayList<String> names = new ArrayList<>();
  43.             ArrayList<Double> grades = new ArrayList<>();
  44.             double avg;
  45.             double total = 0;
  46.             for (Map.Entry<String, Double> name : wholeMap.get(score.getKey()).entrySet()) {
  47.                 names.add(name.getKey());
  48.                 grades.add(name.getValue());
  49.             }
  50.             Collections.sort(names);
  51.             System.out.print(names);
  52.             for (int i = 0; i < grades.size(); i++) {
  53.                 total += grades.get(i);
  54.             }
  55.             avg = total / (double)grades.size();
  56.             System.out.printf("; avg=%.2f\n", avg);
  57.         }
  58.  
  59.     }
  60.     public static ArrayList<String[]> remakeInput(ArrayList<String> list){
  61.         list.remove(0);
  62.         list.remove(0);
  63.         list.remove(0);
  64.         list.remove(list.size() - 1);
  65.  
  66.         ArrayList<String[]> informationLines = new ArrayList<>();
  67.         for (String tableLine : list) {
  68.             String tokens = tableLine.replace('|', ' ').replaceAll("\\s+", " ").trim();
  69.             String[] arrTokens = tokens.split(" ");
  70.             informationLines.add(arrTokens);
  71.         }
  72.         return informationLines;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement