Curtis Schreiner

Curtis Schreiner

  • 1.9k
  • 10
  • 19.9k

Streamreader into list using split

Apr 19 2013 2:51 AM
Hey there,

I figured this would be an easy solution, however I've tried to approach this from many different angles with no success.

Here's a quick breakdown on what I'm trying to accomplish.

1. Read the text file with values separated by commas aka "CSV"
2. Use a while loop to go through the data and split the lines into 3 columns
        a) Loop the read data into a list
        b) Loop the read data onto the textbox

I can print off the first row, however I'm trying to figure out how/why I'm unable to loop the remaining lines within the roster.txt file. Any help would be greatly appreciated. Thanks.


        private void button4_Click(object sender, EventArgs e)
        {
            StreamReader roster = new StreamReader(@"c:\files\roster.txt");

            string roster_data = roster.ReadLine();
            string[] splitter = roster_data.Split(',');
            //string roster_data;

            int x = 0;

            List<string> playerlist = new List<string>();
            //var playerlist = new List<string[]>();


            while (x < splitter.Length)
            {
                string player = Convert.ToString(splitter[x]);
                double salary = Convert.ToDouble(splitter[x + 1]);
                string position = Convert.ToString(splitter[x + 2]);
                string combined = (string.Format("{0} {1:c2} {2}", player, salary, position));


                x = x + 3;

                playerlist.Add(combined);
                textBox1.AppendText(combined);
            }
            roster.Close();

            //while ((roster_data = roster.ReadLine()) != null)
            //{
            //    playerlist.Add(roster_data.Split(','));
            //}
            //foreach (string[] s in playerlist)
            //{
            //    string first = playerlist[0][0];
            //    string second = playerlist[0][1];
            //    string combined = (first + second);

            //    textBox1.AppendText(combined);
            //}
        }

Answers (3)