Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- public class OrderByAge {
- public static class Person {
- private String name;
- private String id;
- private int age;
- public Person(String name, String id, int age) {
- this.name = name;
- this.id = id;
- this.age = age;
- }
- public int getAge() {
- return this.age;
- }
- @Override
- public String toString() {
- return String.format("%s with ID: %s is %d years old.", this.name, this.id, this.age);
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String personalInformation = scanner.nextLine();
- List<Person> listOfPeople = new ArrayList<>();
- while (!personalInformation.equals("End")) {
- String name = personalInformation.split(" ")[0];
- String id = personalInformation.split(" ")[1];
- int age = Integer.parseInt(personalInformation.split(" ")[2]);
- Person person = new Person(name, id, age);
- listOfPeople.add(person);
- personalInformation = scanner.nextLine();
- }
- listOfPeople.sort(Comparator.comparing(Person::getAge));
- for (Person person : listOfPeople) {
- System.out.println(person.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment