Advertisement
nikeza

5.Average Students Grades

Sep 29th, 2019
168
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 map1_Lab_5Average_Students_Grades {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.         Map<String, List<Double>> students = new TreeMap<>();
  8.  
  9.         for (int i = 0; i < n; i++) {
  10.             String[] input = scanner.nextLine().split("\\s+");
  11.             double grade = Double.parseDouble(input[1]);
  12.             students.putIfAbsent(input[0], new ArrayList<>());
  13.             students.get(input[0]).add(grade);
  14.         }
  15.  
  16.         for (Map.Entry<String, List<Double>> entry : students.entrySet()) {
  17.             System.out.print(entry.getKey() + " -> ");
  18.             double avg = 0;
  19.             for (Double s : entry.getValue()) {
  20.                 System.out.printf("%.2f ", s);
  21.                 avg += s;
  22.             }
  23. //            double avg = entry.getValue().stream()
  24. //                    .mapToDouble(Double::doubleValue)
  25. //                    .average()
  26. //                    .getAsDouble();
  27.             System.out.printf("(avg: %.2f)%n", avg / entry.getValue().size());
  28.         }
  29.  }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement