Advertisement
IvaAnd

Lab05_AverageStudentsGrades

Sep 29th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 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. addStudentsInMap(students, input);
  15. }
  16.  
  17. printFormattedResult(students);
  18.  
  19. }
  20.  
  21. public static void printFormattedResult(Map<String, List<Double>> students) {
  22. students.entrySet()
  23. .stream()
  24. .forEach(student -> {
  25. System.out.printf("%s -> ", student.getKey());
  26.  
  27. student.getValue()
  28. .stream()
  29. .forEach(e -> System.out.printf("%.2f ", e));
  30.  
  31. double average = 0;
  32.  
  33. for (Double currGrade : student.getValue()) {
  34. average = average + currGrade;
  35. }
  36. average = average / student.getValue().size();
  37.  
  38. System.out.printf("(avg: %.2f)%n", average);
  39.  
  40. });
  41. }
  42.  
  43. public static void addStudentsInMap(Map<String, List<Double>> students, String[] input) {
  44. String name = input[0];
  45. double grade = Double.parseDouble(input[1]);
  46.  
  47. students.putIfAbsent(name, new ArrayList<>());
  48. students.get(name).add(grade);
  49. }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement