Advertisement
veronikaaa86

05. Students

Jun 22nd, 2023
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package objectsAndClasses;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class P05Students {
  8.     public static class Student {
  9.         String firstName;
  10.         String lastName;
  11.         String age;
  12.         String town;
  13.  
  14.         public Student (String firstName, String lastName, String 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 String getAge() {
  34.             return this.age;
  35.         }
  36.     }
  37.  
  38.     public static void main(String[] args) {
  39.         Scanner scanner = new Scanner(System.in);
  40.  
  41.         List<Student> studentsList = new ArrayList<>();
  42.  
  43.         String input = scanner.nextLine();
  44.         while (!input.equals("end")) {
  45.             String[] studentsDataArr = input.split(" ");
  46.             String firstName = studentsDataArr[0];
  47.             String lastName = studentsDataArr[1];
  48.             String age = studentsDataArr[2];
  49.             String town = studentsDataArr[3];
  50.  
  51.             Student currentStudent = new Student(firstName, lastName, age, town);
  52.  
  53.             studentsList.add(currentStudent);
  54.  
  55.             input = scanner.nextLine();
  56.         }
  57.  
  58.         String homeTown = scanner.nextLine();
  59.  
  60.         for (Student student: studentsList) {
  61.             if (student.getTown().equals(homeTown)) {
  62.                 System.out.printf("%s %s is %s years old%n",
  63.                         student.getFirstName(), student.getLastName(), student.getAge());
  64.             }
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement