I'm trying to read each line of a file, line by line, and place these individual lines, divided further into an array of their individual "words" into a jagged array so that I can further manipulate them. If anyone is familiar I am attempting Day 12 of the Advent of Code Challenge so my inputs look like
0 <-> 780, 1330
1 <-> 264, 595, 1439
2 <-> 296, 531, 1440
and I would like each line to be
array[0][0] = 0
array[0][1] = <->
array[0][2] = 780
array[0][3] = 1330
array[1][0] = 1
... etc
I have been working on this for like a day now and I can't seem to get C# to do this. Here is the latest stab at a solution I have been trying
static void Main(string[] args)
{
string[][] input = new string[2000][];
string line;
System.IO.StreamReader file =
System.IO.StreamReader(@"c:/theFilePath.txt");
while ((line = file.ReadLine()) != null)
{
for (int i = 0; i < 2000; i++)
{
string[] eachArray = line.Split(null);
input[i] = new string[] { eachArray };
}
}
for (int i = 0; i < input.Length; i++)
{
for (int j = 0; j < input[i].Length; j++)
{
Console.WriteLine(input[i][j]);
}
}
}
and the error I'm getting is "Cannot implicitly convert type string[] to string"
But shouldn't each line of the jagged array be a string[]
and not a string
already?
I must be missing some basic aspect of what is going on behind the scenes here.
I have tried using File.ReadAllLines
and File.ReadLines
and parsing them as string arrays too but the problem always comes down to instantiating the array in the loop. Can't seem to make that work.
Any other methods to solve this in C# would be welcome as well, I would just like to understand this problem better.