Click here to Skip to main content
14,447,269 members
Rate this:
Please Sign up or sign in to vote.
See more:
example of txt file Line bile line into list of singers which 1 singer have list of songs..From Singer.txt..

John|Newman|Singer|09112000|Male|song1;song2;song3
Ezrea|Leusag|Singer|09112020|Male|song1;song2;song3

List<singer>

class Singer
Name
Surname
Singer
DateOfBirth
Gendre
List<songs>
---Songs object have property SongName

in one way i need to fullfill this line into List<singer> who have list<songs>

What I have tried:

using (StreamReader str = new StreamReader(@"C:\Singer.txt"))
            {
                char[] delimiterChars = { '|', ';'};
                
                while ((line = str.ReadLine()) != null)
                {
                    string[] word = line.Split(delimiterChars);

                    listSinger.Add(new Singer(
                        int.Parse(
                            word[0]), 
                            word[1], 
                            word[2], 
                            word[3], 
                            DateTime.ParseExact(word[4], "ddmmyyyy", CultureInfo.InvariantCulture)));
                }                
            }
Posted
Updated 22-May-18 4:38am
Comments
Christian Graus 14-May-18 19:19pm
   
That code looks reasonable, assuming that constructor exists. Why doesn't it work?
Rate this:
Please Sign up or sign in to vote.

Solution 1

your code is wrong judging by your data.
John|Newman|Singer|09112000|Male|song1;song2;song3
Ezrea|Leusag|Singer|09112020|Male|song1;song2;song3

your
0=first name, guessing, should be string
1=last name, guessing, should be string
2=Occupation(guessing), should be string
3=zero padded date, guessing, ddmmyyyy formatted 8 bytes, should be string.
4=Sex, Should be string
5=semicolon separated song title, again guessing, should be string

I can see a constructor, that looks like
new Singer(int, string, string, string, DateTime)

Here you are trying to parse first name to int
index 4 looks like Sex(Male/Female), you are trying to parse as DateTime
for both of these above issues exception should be thrown.


I hope you can guess from this what you needed to do
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

First of all, i'd suggest to use File.ReadAllLines Method (String) (System.IO)[^].
Your Singer class should looks like:
public class Singer
{
	private string sFName = string.Empty;
	private string sSurname = string.Empty;
	private string sRole  = string.Empty;
	private DateTime dDateOfBirth = new DateTime(1901, 1, 1); //default value
	private string sGenre = string.Empty;
	private List<string> songs = new List<string>();
	
        //instantiate singer from single line
	public Singer(string line)
	{
		string[] parts = line.Split(new string[]{"|"}, StringSplitOptions.RemoveEmptyEntries);
		sFName = parts[0];
		sSurname = parts[1];
		sRole = parts[2];
		dDateOfBirth = DateTime.ParseExact(parts[3], "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
		sGenre = parts[4];
		songs = parts[5].Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries).ToList();
	}
	
         //instantiate singer from parts
	public Singer(string _FName, string _Surname, string _Role, DateTime _DOB, string _Genre, List<string> _songs)
	{
		sFName = _FName;
		sSurname = _Surname;
		sRole = _Role;
		dDateOfBirth = _DOB;
		sGenre = _Genre;
		songs = _songs;
	}

	public string FirstName
	{
		get { return sFName; }
		set { sFName = value; }
	}

	public string Surname
	{
		get { return sSurname; }
		set { sSurname = value; }
	}
	
	public string Role
	{
		get { return sRole; }
		set { sRole = value; }
	}
	
	public DateTime DateOfBirth
	{
		get { return dDateOfBirth; }
		set { dDateOfBirth = value; }
	}
	
	public string Genre
	{
		get { return sGenre; }
		set { sGenre = value; }
	}	

	public List<string> Songs
	{
		get { return songs; }
		set { songs = value; }
	}	

}


Usage:
string[] lines = File.ReadAllLines("FullFileName.txt");

List<Singer> singers = lines.Select(s=> new Singer(s)).ToList();
foreach(Singer s in singers)
{
    Console.WriteLine("{0} {1}, count of songs: {2}", s.FirstName, s.Surname, s.Songs.Count);
}


Result:
John Newman, count of songs: 3
Ezrea Leusag, count of songs: 3
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100