desislava_topuzakova

Students -> Main

Feb 21st, 2021
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package Students_05;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.         List<Student> students = new ArrayList<>();
  14.         for (int i = 1; i <= n ; i++) {
  15.             String studentsData = scanner.nextLine();
  16.             //"Lakia Eason 3.90" -> split(" ") -> ["Lakia", "Eason", "3.90"]
  17.             String [] splittedData = studentsData.split("\\s+"); //["Lakia", "Eason", "3.90"]
  18.             String firstName = splittedData[0];
  19.             String lastName = splittedData[1];
  20.             double grade = Double.parseDouble(splittedData[2]);
  21.  
  22.             Student student = new Student(firstName, lastName, grade);
  23.             students.add(student);
  24.         }
  25.  
  26.        List<Student> sortedList = students.stream()
  27.                .sorted(Comparator.comparing(Student::getGrade).reversed())
  28.                .collect(Collectors.toList());
  29.  
  30.         for (Student student : sortedList ) {
  31.             System.out.println(student.toString());
  32.         }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment