Advertisement
nkostadinski

AverageStudentsGrades

Sep 26th, 2021
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class _05_AverageStudentsGrades {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         TreeMap<String, List<Double>> students = new TreeMap<>();
  8.  
  9.         int n = Integer.parseInt(scan.nextLine());
  10.  
  11.         while (n-- > 0) {
  12.             String[] tokens = scan.nextLine().split("\\s+");
  13.             String student = tokens[0];
  14.             double grade = Double.parseDouble(tokens[1]);
  15.  
  16.             students.putIfAbsent(student, new ArrayList<>());
  17.             students.get(student).add(grade);
  18.  
  19.         }
  20.  
  21.         students.entrySet()
  22.                 .stream()
  23.                 .forEach(entry -> {
  24.                     double sum = 0;
  25.                     for (int i = 0; i < entry.getValue().size(); i++) {
  26.                         sum += entry.getValue().get(i);
  27.                     }
  28.                     double avgSum = sum / entry.getValue().size();
  29.  
  30.                     System.out.print(entry.getKey() + " -> ");
  31.                     entry.getValue().forEach(e -> System.out.printf("%.2f ", e));
  32.                     System.out.printf("(avg: %.2f)%n", avgSum);
  33.                 });
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement