Advertisement
Guest User

Object and Classes - Students 2.0 Error

a guest
Jul 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. namespace Objects_and_Classes
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<Student> students = new List<Student>();
  13.  
  14.             string input = Console.ReadLine();
  15.             while (input != "end")
  16.             {
  17.                 string[] tokens = input.Split(" ");
  18.                 string firstName = tokens[0];
  19.                 string lastName = tokens[1];
  20.                 int age = int.Parse(tokens[2]);
  21.                 string city = tokens[3];
  22.                 if (students.Count > 0)
  23.                 {
  24.                     for (int i = 0; i < students.Count; i++)
  25.                     {
  26.                         if (firstName == students[i].FirstName && lastName == students[i].LastName)
  27.                         {
  28.                             students[students.Count - 1].Age = age;
  29.                             students[students.Count - 1].Hometown = city;
  30.                         }
  31.                         else
  32.                         {
  33.                             Student student = new Student();
  34.                             student.FirstName = firstName;
  35.                             student.LastName = lastName;
  36.                             student.Age = age;
  37.                             student.Hometown = city;
  38.                             students.Add(student);
  39.                             input = Console.ReadLine();
  40.                             break;
  41.                         }
  42.                     }
  43.                 }
  44.  
  45.                 input = Console.ReadLine();
  46.             }
  47.             string cityEnd = Console.ReadLine();
  48.             foreach (Student student in students)
  49.             {
  50.                 if (cityEnd == student.Hometown)
  51.                 {
  52.                     Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement