I\'m unable to input the data into a list. It reads the first line, but doesn\'t
ID: 3728781 • Letter: I
Question
I'm unable to input the data into a list. It reads the first line, but doesn't read it all the way through. help?
1. Arrange them in order with age as the primary key( descending order), last name as the secondary key(ascending (alphabetical) order), with the tertiary key being first name(ascending (alphabetical) order). For example:
Smith David 19
Jones Andrew 16
Jones Russell 16
My Code:
namespace homework_file_io
{
public class Person : IComparable
{
string firstname;
string lastname;
int age;
public string FirstName{
get { return firstname; }
set { firstname = value;}
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public Person(string fName,string lName,int yourAge )
{
firstname = fName;
lastname = lName;
age = yourAge;
}
public override string ToString()
{
return string.Format("[{0}, {1}], {2}",firstname,lastname,age);
}
int IComparable.CompareTo(object obj)
{
Person person = (Person)obj;
if(person.age == this.age)
{
if(person.lastname.CompareTo(this.lastname) == 0)
{
return this.firstname.CompareTo(person.firstname);
}
return this.lastname.CompareTo(person.lastname);
}
else if((person.age.CompareTo(this.age)) != 0)
{
return this.age;
}
return person.age.CompareTo(this.age);
}
}
}
static void Main(string[] args)
{
string fileName = "inputdata.txt";
string fileNameOutput = "outputdata.txt";
try
{
StreamReader reader = new StreamReader(fileName);
StreamWriter writer = new StreamWriter(fileNameOutput);
Console.WriteLine("File:{0} Is open",fileName);
string data = reader.ReadLine();
List<Person> people = new List <Person>();
using (reader)
{
while (reader.EndOfStream == false)
{
Person person = new Person("Paris", "Walters", 00);
string[] delimiter = data.Split(',');
person.LastName = delimiter[0].Trim();
person.FirstName = delimiter[1].Trim();
person.Age = int.Parse(delimiter[2].Trim());
people.Add(person);
}
people.Sort();
}
foreach (Person s in people)
{
Console.WriteLine(s);
}
using(writer)
{
foreach (Person lines in people)
{
writer.WriteLine(lines);
}
}
}
catch(FileNotFoundException)
{
Console.Error.WriteLine("File Not Found: {0}",fileName);
}
}
}
Explanation / Answer
Your have used :
string data = reader.ReadLine();
before while loop. This will store first line in the data but you have not updated this data inside while loop, thus will always contain first line of the input file. data should be updated inside loop, in the end of the loop so that it reads next line in the input file. Also if that doen't work instead of using reader.EndOfStream, try to use data inside loop as below:
while (data)
{
Person person = new Person("Paris", "Walters", 00);
string[] delimiter = data.Split(',');
person.LastName = delimiter[0].Trim();
person.FirstName = delimiter[1].Trim();
person.Age = int.Parse(delimiter[2].Trim());
people.Add(person);
data = reader.ReadLine();
}