3
Answers

Streamreader into list using split

Curtis Schreiner

Curtis Schreiner

11y
4.9k
1
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)
1
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 15y

I don't know how you are doing it so I don't know if there is an easier way. My guess is that there is not an easier way.
 
Note that often the "secret" to making a program that is easy to use is complex development. In other words, sometimes we must write complicated program to make one that is easy to use. Windows and .Net and database software such as SQL Server does a lot for us, but sometimes we must do some programming outselves.
 
My guess is that the code necessary for the search page is not difficult except for many details that need to be done.
 
Probably what I would do is to create a List<String> for each filter (selection criteria) then concatenate them all together as needed.
0
Vikas Ahlawat

Vikas Ahlawat

NA 577 661.6k 15y
Ya code is not difficult.
Thanks for your reply.