Advertisement
veronikaaa86

05. Students

Oct 26th, 2022
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package classes;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class P05Students {
  8.     static class Student {
  9.         private String firstName;
  10.         private String lastName;
  11.         private int age;
  12.         private String town;
  13.  
  14.         public Student(String firstName, String lastName, int age, String town) {
  15.             this.firstName = firstName;
  16.             this.lastName = lastName;
  17.             this.age = age;
  18.             this.town = town;
  19.         }
  20.  
  21.         public String getTown() {
  22.             return this.town;
  23.         }
  24.  
  25.         public String getFirstName() {
  26.             return this.firstName;
  27.         }
  28.  
  29.         public String getLastName() {
  30.             return this.lastName;
  31.         }
  32.  
  33.         public int getAge() {
  34.             return this.age;
  35.         }
  36.     }
  37.  
  38.  
  39.     public static void main(String[] args) {
  40.         Scanner scanner = new Scanner(System.in);
  41.  
  42.         List<Student> studentsList = new ArrayList<>();
  43.         String inputLine = scanner.nextLine();
  44.         while (!inputLine.equals("end")) {
  45.             String[] arrStudentData = inputLine.split("\\s+");
  46.             String firstName = arrStudentData[0];
  47.             String lastName = arrStudentData[1];
  48.             int age = Integer.parseInt(arrStudentData[2]);
  49.             String town = arrStudentData[3];
  50.  
  51.             Student currentStudent = new Student(firstName, lastName, age, town);
  52.             studentsList.add(currentStudent);
  53.  
  54.             inputLine = scanner.nextLine();
  55.         }
  56.  
  57.         String homeTown = scanner.nextLine();
  58.  
  59.         for (Student item : studentsList) {
  60.             if (item.getTown().equals(homeTown)) {
  61.                 System.out.printf("%s %s is %d years old%n", item.getFirstName(), item.getLastName(), item.getAge());
  62.             }
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement