0

I am using C# in a console application.

I need to load data from a text file and load it into a 2d array.

This is what I tried, but when I try to print out the contents of what gets returned nothing gets printed.

public static int[,] LoadMap()
{
    const string path = @"1.txt";

    string[] fileLines = File.ReadAllLines(path);
    int[,] map = new int[fileLines.Length, 15];
    string line;
    for (int i = 0; i < fileLines.Length; ++i)
    {
        line = fileLines[i];
        for (int j = 0; j < line.Length; ++j)
        {
            map[i, j] = (int)(line[j] - '0');
        }
    }

    return map;
}

But when I hardcode the data like that, then everything gets displayed perfectly.

private static int[,] Map = new int[MapX, MapY]
{
            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

The data in the text file looks like that :

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

Any help will be appreciated whether you fix what I tried or propose something completely different, thanks.

4 Answers 4

3
 string[] fileLines = File.ReadAllLines(path);
        int[,] map = new int[fileLines.Length,fileLines[0].Split(',').Length];
        for (int i = 0; i < fileLines.Length; ++i)
        {
            string line = fileLines[i];
            for (int j = 0; j < map.GetLength(1); ++j)
            {
                string[] split = line.Split(',');
                map[i, j] = Convert.ToInt32(split[j]);
            }
        }
        return map;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You could use LINQ to parse the lines:

var lines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 25];
for (int i = 0; i < fileLines.Length; ++i)
{
    var data = lines[i].Split(',').Select(c => Convert.ToInt32(c)).ToList();
    for(int j =0; j<25; ++j)
       map[i,j] = data[j];
}
return map;

If you could use a jagged array instead of a 2D array, this becomes simpler:

public static int[][] LoadMap()
{
    return File.ReadLines(path)
             .Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
             .ToArray();
}

1 Comment

Thank you Reed, I use the first code you proposed and altered it a bit now it works perfectly !
0

If your text file has commas separating the values, replace this line:

for (int j = 0; j < line.Length; ++j)

With:

for (int j = 0; j < line.Length; j += 2)

That's assuming your values will always be only 1 char long.

Comments

0

When the data comes in from the text file. If it is separated by a comma you can separate it using string.split. Then you load what you get into an array and access it like you normally would do an array. Like below:

  string[] lines = System.IO.File.ReadAllLines(@"path");
  foreach (string line in lines)
  {  
      string[] first= line.Split(comma);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.