-2

I have this code that reads a csv file and on every line it uses line.split(',') to sort each part of one line into values, a string array.

How would I then put "values[1]" into it's own array?

static void Main(string[] args)
{
    string line;
    string[] countrys;

    using (StreamReader sr = new StreamReader("Countries_database.csv"))
    {
        try
        {
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine();
                var values = line.Split(',');
                Console.WriteLine(values[1]);
            }
            countrys = values[1];
        }
        catch(Exception ex)
        {
            Console.WriteLine("The file could not be read {0}", ex);
        }

    }
}

The countrys = values[1]; does not work but it just shows what I am aiming to do.

Many Thanks!

Shogan
  • 1,154
  • 1
  • 10
  • 24
  • Think you'll need to explain what you are expecting a bit more.. Values[1] will be a `string`, are you wanting 1 string value in a random array? are you wanting value 1 from each line in the same array? – Matt Sep 24 '19 at 21:03
  • Aren't you doing that already with `countrys = values[1];`? – Sach Sep 24 '19 at 21:07
  • Are you trying to parse a CSV file? `line.Split(',')` [doesn't do that](http://www.secretgeek.net/csv_trouble) but there are plenty of [tools available that do](https://stackoverflow.com/questions/2081418/). – Dour High Arch Sep 24 '19 at 21:10
  • you could either initialize a new array with only one member or you could further split the string into another array. But without knowing what you're trying to do i can't say which. var countrys = values.Split(','); << this will split your values into it's own array OR string[] countrys = new string[] { values }; << this will place your values string at position 0 in a new array. – hexagod Sep 24 '19 at 21:14

2 Answers2

2

How about a list? That way, you don't have to worry about the indexing / sizing.

List<string> countries = new List<string>();
...//read stuff
countries.Add(values[1]); //add the string to the list
devlin carnate
  • 8,309
  • 7
  • 48
  • 82
2

Your values array object is out of scope to assign it to countrys - i.e. where you do:

countrys = values[1];

Your var values = line.Split(','); will create values only in the scope of that iteration of your while loop. So you should ideally add it to a list that is declared outside of the while loop, or assign it once in the while loop and then break out of the while loop.

Something like this might work better:

static void Main(string[] args)
{
    string line;
    List<string> countrys = new List<string>();

    using (StreamReader sr = new StreamReader("Countries_database.csv"))
    {
        try
        {
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine();
                var values = line.Split(',');
                Console.WriteLine(values[1]);
                countrys.Add(values[1]);
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine("The file could not be read {0}", ex);
        }

    }
}
Shogan
  • 1,154
  • 1
  • 10
  • 24