Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * Created by snap2n on 13-Nov-15.
- */
- public class ExamScore {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- ArrayList<String> tableLines = new ArrayList<>();
- // String inputLine = sc.nextLine();
- while (sc.hasNextLine()){
- tableLines.add(sc.nextLine());
- // inputLine = sc.nextLine();
- }
- TreeMap<Integer, TreeMap<String, Double>> wholeMap = new TreeMap<>();
- ArrayList<String[]> tokens = remakeInput(tableLines);
- for (String[] token : tokens) {
- StringBuilder currentStudentSB = new StringBuilder();
- currentStudentSB.append(token[0]);
- currentStudentSB.append(" ");
- currentStudentSB.append(token[1]);
- String currentStudent = currentStudentSB.toString();
- int currentExamScore = Integer.parseInt(token[2]);
- double currentGrade = Double.parseDouble(token[3]);
- if (!wholeMap.containsKey(currentExamScore)){
- wholeMap.put(currentExamScore, new TreeMap<>());
- wholeMap.get(currentExamScore).put(currentStudent, currentGrade);
- }else{
- if (!wholeMap.get(currentExamScore).containsKey(currentStudent)){
- wholeMap.get(currentExamScore).put(currentStudent, currentGrade);
- }
- }
- }
- for (Map.Entry<Integer, TreeMap<String, Double>> score: wholeMap.entrySet()) {
- System.out.printf("%d -> ", score.getKey());
- ArrayList<String> names = new ArrayList<>();
- ArrayList<Double> grades = new ArrayList<>();
- double avg;
- double total = 0;
- for (Map.Entry<String, Double> name : wholeMap.get(score.getKey()).entrySet()) {
- names.add(name.getKey());
- grades.add(name.getValue());
- }
- Collections.sort(names);
- System.out.print(names);
- for (int i = 0; i < grades.size(); i++) {
- total += grades.get(i);
- }
- avg = total / (double)grades.size();
- System.out.printf("; avg=%.2f\n", avg);
- }
- }
- public static ArrayList<String[]> remakeInput(ArrayList<String> list){
- list.remove(0);
- list.remove(0);
- list.remove(0);
- list.remove(list.size() - 1);
- ArrayList<String[]> informationLines = new ArrayList<>();
- for (String tableLine : list) {
- String tokens = tableLine.replace('|', ' ').replaceAll("\\s+", " ").trim();
- String[] arrTokens = tokens.split(" ");
- informationLines.add(arrTokens);
- }
- return informationLines;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement