0

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.

ASh
  • 34,632
  • 9
  • 60
  • 82
Mike Weiss
  • 309
  • 3
  • 11
  • i suppose it should be simply `input[i] = eachArray;`. array initializer (`new string[] { }`) can have only `string` values inside { } – ASh Dec 20 '17 at 14:06
  • I tried that too, but the problem is it will continue to print the same line over and over. Properly separated into indexes but it never advances to the next line of the file. – Mike Weiss Dec 20 '17 at 14:08
  • `line = file.ReadLine();` should be in `for` loop then. – ASh Dec 20 '17 at 14:10
  • That worked, thank you! – Mike Weiss Dec 20 '17 at 14:14
  • `input[i] = GetWords(line)`, (GetWords is [here](https://stackoverflow.com/a/16734675/1997232)) and you don't need nested loop. – Sinatr Dec 20 '17 at 14:15

1 Answers1

1

File class from System.IO has method ReadAllLines which can be used here. To split each line and convert to array use Select and ToArray Linq extension methods (add required usings to access mentioned methods)

using System.IO;
using System.Linq;

string[][] input = File.ReadAllLines(@"c:/theFilePath.txt")
                       .Select(x => x.Split(' '))
                       .ToArray();
ASh
  • 34,632
  • 9
  • 60
  • 82
  • This is even more succinct. Thanks for the help! – Mike Weiss Dec 20 '17 at 14:31
  • @MikeWeiss, yes, Linq often helps to shorten code, previously written in loop[s]. if my answer helped, please mark it accepted – ASh Dec 20 '17 at 14:33
  • Sorry, still a bit new to all of this, marked as accepted now, and I'll definitely be taking a closer look at Linq – Mike Weiss Dec 20 '17 at 14:35
  • good luck, take a look here [introduction-to-linq-queries](https://learn.microsoft.com/en-Us/dotnet/csharp/programming-guide/concepts/linq/introduction-to-linq-queries) – ASh Dec 20 '17 at 14:39