using split function to separate integer and string and populate 2 different list

user1

New member
Joined
Nov 18, 2014
Messages
3
Programming Experience
Beginner
Hi, I am trying to separate data using the split function and to send the data to two different list, one integer and one string list. I'm stuck on this part and would be grateful if someone could explain the process to me. I'll be pasting the code below.

Thank you for your time.

C#:
private void Form1_Load(object sender, EventArgs e)
        {            
            StreamReader Reader;
            Reader = File.OpenText("Scores.txt");

            //these are the data found in scores.txt
             /*  Anna,87
                John,98
                Paula,67
                Melissa,82 */

            List<string> listName = new List<string>();

            string line;
            while ((line = Reader.ReadLine()) != null)
            {
                string[] split = line.Split(',');

            }
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Personally I would not split it to two lists. There is always a risk that e.g. a name is removed from the name list so the data will be out of sync. I would rather look at a datatable or dictionary.

Further I'm not sure what your stuck with? Conversion from string to int? How to create the second list? But anyway, below code should get you on the way for two lists.

            List<string> listNames = new List<string>();
            List<int> listScores = new List<int>();

            string input = "Melissa, 73";
            string[] values = input.Split(',');

            int score;
            if (!Int32.TryParse(values[1], out score))
            {
                // error handling; bail out
            }

            listNames.Add(values[0]);
            listScores.Add(score);
 

user1

New member
Joined
Nov 18, 2014
Messages
3
Programming Experience
Beginner
Thank you, that's what i was trying to do, but got stuck on this

listNames.Add(values[0]);
listScores.Add(score);
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
string[] values is split on the "," char so it takes the line from the reader and splits the content into however many "," char's there are....in your case the will be 2 values in values...the name (values[0]) and the integer (values[1]) the [0] and [1] are index values for the position of the value in values.....

you could also write the code like this for example:

private void Form1_Load(object sender, EventArgs e)
        {            
            StreamReader Reader;
            Reader = File.OpenText("Scores.txt");

            //these are the data found in scores.txt
             /*  Anna,87
                John,98
                Paula,67
                Melissa,82 */

            List<string> listName = new List<string>();
            List<int> scores = new List<int>();

            string line;
            while ((line = Reader.ReadLine()) != null)
            {
                string[] split = line.Split(',');
                listName.Add(split[0]);
                scores.Add(Convert.ToInt32(split[1]));

            }


also you should read about the Dictionary , the Dictionary stores key value pairs...so the dictionary will have the name as a key and the score as a value....very powerful!

Hope this helps.

-InkedGFx
 
Top Bottom