P_Donchev

SoftUni Problems - Average Student Grades

Oct 3rd, 2020 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AverageStudentGrades {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         TreeMap<String, ArrayList<Double>> students = getStudents(scan);
  7.  
  8.         students.entrySet().forEach(entry -> {
  9.             System.out.print(entry.getKey() + " -> ");
  10.             entry.getValue().forEach(innerEntry -> System.out.printf("%.2f ", innerEntry));
  11.             System.out.printf("(avg: %.2f)%n", entry.getValue().stream().mapToDouble(e -> e).average().getAsDouble());
  12.         });
  13.     }
  14.  
  15.     private static TreeMap<String, ArrayList<Double>> getStudents(Scanner scan) {
  16.         int n = Integer.parseInt(scan.nextLine());
  17.         TreeMap<String, ArrayList<Double>> students = new TreeMap<>();
  18.  
  19.         for (int i = 0; i < n; i++) {
  20.             String[] tokens = scan.nextLine().split("\\s+");
  21.             String name = tokens[0];
  22.             Double grade = Double.parseDouble(tokens[1]);
  23.  
  24.             students.putIfAbsent(name, new ArrayList<>());
  25.             students.get(name).add(grade);
  26.         }
  27.  
  28.         return students;
  29.     }
  30. }
  31.  
Add Comment
Please, Sign In to add comment