Advertisement
IvaAnd

Sets and Maps_Lab05_AverageStudentsGrades_average with loop

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