0

I have been unable to apply any solutions to this issue. The exception happens to this line here: currentMap[row, col] = Int32.Parse(s); What I am wanting to do is pass this method a specific file storing rows of numbers like this:

1,1,1
1,0,1
1,1,1

I then want each number to be stored in int[,] currentMap which gets returned. The file I am using contains no large numbers. I think that the size of array I am creating is right and so I don't understand why this isn't working. I am used to doing similar stuff using NextInt in java but I couldn't find any alternative for c#.

Thanks for any help.

private int[,] LoadMapArray(String filename)
    {
        int[,] currentMap;

        int rows = 0;
        int cols = 0;

        StreamReader sizeReader = new StreamReader(filename);

        using (var reader = File.OpenText(filename))
        {
            while (reader.ReadLine() != null)
            {
                string line = sizeReader.ReadLine();
                cols = line.Length;
                rows++;
            }
        }
        currentMap = new int[rows,cols];

        StreamReader sr = new StreamReader(filename);

        for (int row = 0; row < rows + 1; row++)
        {
            string line = sr.ReadLine();
            string[] split = new string[] {","};
            string[] result;

            result = line.Split(split, StringSplitOptions.None);

            int col = 0;

            foreach (string s in result)
            {
                currentMap[row, col] = Int32.Parse(s);
                col++;
            }
        }

        return currentMap;
    }

Edit: Code was fixed after changing how I was accessing the file. I then had to change this to catch null:

for (int row = 0; row < rows + 1; row++)
        {
            string line = sr.ReadLine();
            string[] split = new string[] { "," };
            string[] result;

            if (line != null)
            {
                result = line.Split(split, StringSplitOptions.None);

                int col = 0;

                foreach (string s in result)
                {
                    currentMap[row, col] = Int32.Parse(s);
                    col++;
                }
            }


        }
user3622946
  • 3
  • 1
  • 3

1 Answers1

2

No, the size of your array is not correct. You read two lines at each loop but you increment the rows counter just one time.

    using (var reader = File.OpenText(filename))
    {
        string line = string.Empty;
        while ((line = reader.ReadLine()) != null)
        {
            rows++;
        }
    }

And I am sure that also the cols count is not correct, but it doesn't raise an exception because you are dimensioning the cols dimension bigger than required. (You count also the space for the commas, not just the numbers)

A simpler approach (if your file is not very big) is to use File.ReadAllLines()

string[] split = new string[] {","};
string[] lines = File.ReadAllLines(filename);
int rows = lines.Length;
int cols = lines[0].Split(split, StringSplitOptions.RemoveEmptyEntries).Count();
currentMap = new int[rows,cols];

for (int row = 0; row < rows; row++)
{
      string line = lines(row);
      string[] result = line.Split(split, StringSplitOptions.None);
      int col = 0;
      foreach (string s in result)
      {
           int value;
           Int32.TryParse(s, out value)
           currentMap[row, col] = value;
           col++;
      }
 }

Now, the entire file is in memory with just one disk operation and you could work using the in memory strings. The parsing of the integer should be changed to use Int32.TryParse to avoid exception in case the retrieved value is not a valid integer.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks for the good reply. I tried the code out and still got the same issue so I went and fixed the way I was loading the file. Turns out that was the issue so I am sorry for the pointless post. After that I managed to get my original code to work by first checking if line was null. I will edit that into my original question. Everything still works fine and I can get what I want from it even though my array is off. I will change that and stick with my code as I don't know how big I will make the text file. The try parse isn't needed as I will make sure the textfile itself can be parsed.Thanks – user3622946 Jun 10 '14 at 11:33