Advertisement
Guest User

Untitled

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