I could not get the Quick Joe Smith response to work, so I modified it. I put the modified code in a static method in the "FileReader" class:
public static double[][] readWhitespaceDelimitedDoubles(string[] input) { double[][] array = input.Where(line => !String.IsNullOrWhiteSpace(line)) // Use this to filter blank lines. .Select(line => line.Split((string[])null, StringSplitOptions.RemoveEmptyEntries)) .Select(line => line.Select(element => double.Parse(element))) .Select(line => line.ToArray()) .ToArray(); return array; }
For my application, I understood double, not int. To call the code, try using something like this:
string[] fileContents = System.IO.File.ReadAllLines(openFileDialog1.FileName); double[][] fileContentsArray = FileReader.readWhitespaceDelimitedDoubles(fileContents); Console.WriteLine("Number of Rows: {0,3}", fileContentsArray.Length); Console.WriteLine("Number of Cols: {0,3}", fileContentsArray[0].Length);
source share