Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Lab05_AverageStudentsGrades {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<Double>> students = new TreeMap<>();
- int numberOfStudents = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < numberOfStudents; i++) {
- String[] input = scanner.nextLine().split(" ");
- addStudentsInMap(students, input);
- }
- printFormattedResult(students);
- }
- public static void printFormattedResult(Map<String, List<Double>> students) {
- students.entrySet()
- .stream()
- .forEach(student -> {
- System.out.printf("%s -> ", student.getKey());
- student.getValue()
- .stream()
- .forEach(e -> System.out.printf("%.2f ", e));
- double average = 0;
- for (Double currGrade : student.getValue()) {
- average = average + currGrade;
- }
- average = average / student.getValue().size();
- System.out.printf("(avg: %.2f)%n", average);
- });
- }
- public static void addStudentsInMap(Map<String, List<Double>> students, String[] input) {
- String name = input[0];
- double grade = Double.parseDouble(input[1]);
- students.putIfAbsent(name, new ArrayList<>());
- students.get(name).add(grade);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement