Converting a list of "word,word\n" in a txt file to an array [,] - getting error

-1

I have a list of postal codes and the cities the codes are for in a text file. The data looks like this:

2450,København SV
2500,Valby
2600,Glostrup
2605,Brøndby
2610,Rødovre
2625,Vallensbæk
2630,Taastrup
2635,Ishøj
2640,Hedehusene

There are 580 lines of text there.

I started by converting the text to jagged array[][] but that's not really meeting my needs farther along in my code. So a simple array[,] is preferable.

Unfortunately I'm apparently too new in c# to be able to get there myself.

        string testing = File.ReadAllText(@"U:\Testing.txt");
        int i = 0, j = 0;
        string[,] result = new string[580, 2];
        foreach (var row in testing.Split('\n'))
        {
            j = 0;
            foreach (var col in row.Trim().Split(','))
            {
                result[i, j] = col.Trim();
                j++;  //Line 26 - this is where I get the exception error
            }
            i++;
        }

I can't figure out why I'm getting the following error and I've begun tearing out my hair. Any ideas??

System.IndexOutOfRangeException
  HResult=0x80131508
  Message=Index was outside the bounds of the array.
  Source=Testing
  StackTrace:
   at Testing.Analysis.Main() in U:\Uddannelse\Testing\Testing\Program.cs:line 26
c#
string
multidimensional-array
asked on Stack Overflow Oct 7, 2019 by P01y6107 • edited Oct 7, 2019 by Olivier Rogier

3 Answers

3

You are getting the error because somewhere in your file, some rows have a comma in the city name.

If you want to get the whole name, try something like this -

var row = "2450,København, SV"
var values = row.Split(new[] {','}, 2);
//With the extra int param to the Split function, you are saying no matter how many substrings you can form , only give me this specific number of substrings.
//With a value of 2, you are essentially splitting at the first instance of the comma.  

This will give you two values, the first being "2450" and the second "København, SV"

This is assuming that every row has atleast a comma, if not, you'll need to put in a check for it as well.

answered on Stack Overflow Oct 7, 2019 by Rakesh
0

Here is one way you can approach this:

string file_path = "...";
//read all lines from the file
var lines = File.ReadAllLines(file_path);

//You could also use StreamReader to read the file in line by line

string[,] result = new string[lines.Length, 2];
string line;
char[] separator = new char[] { ',' };

//loop over the lines until the end of the file
for(int current_line = 0; current_line < lines.Length; current_line++)
{
    //second argument limits you to two parts, so additional commas will not cause issues
    var parts = line.Trim().Split(separator, 2); 
    //make sure the data was in your expected format (i.e. two parts)
    if(parts.Length == 2)
    {
        result[current_line, 0] = parts[0];
        result[current_line, 1] = parts[1];
    }
}
answered on Stack Overflow Oct 7, 2019 by danielm • edited Oct 7, 2019 by danielm
0

You can try this that corrects the indexing.

static void Test()
{
  var testing = File.ReadAllLines(@"c:\Testing.txt");
  string[,] result = new string[testing.Length, 2];
  int i = 0, j = 0;
  foreach ( var line in testing )
  {
    j = 0;
    foreach ( var col in line.Trim().Split(',') )
      result[i, j++] = col.Trim();
    i++;
  }
  for ( int index = result.GetLowerBound(0); index < result.GetUpperBound(0); index++ )
      Console.WriteLine($"Code = {result[index, 0]}, Name = {result[index,1]}");
}
answered on Stack Overflow Oct 7, 2019 by Olivier Rogier

User contributions licensed under CC BY-SA 3.0