0
votes
  string[] words;
  numOfMatrix = int.Parse(fileIn.ReadLine());

  nameOfMatrix1 = fileIn.ReadLine();
  words = fileIn.ReadLine().Split(' ');
  matrix1H = int.Parse(words[0]);
  matrix1W = int.Parse(words[1]);
  matrix1 = new int[matrix1H + 1, matrix1W + 1];
  for (int i = 1; i <= matrix1H; i++)
  {
    int k = 0;
    words = fileIn.ReadLine().Split(' ');
    for (int j = 1; j <= matrix1W; j++)
    {
      matrix1[i,j] = int.Parse(words[k]);
      k++;
    }
  }

Input Sample Data

3
Matrix One
5 7
45   38    5   56   18   34    4
87   56   23   41   75   87   97
45   97   86    7    6    8   85
67    6   79   65   41   37    4
 7   76   57   68    8   78    2
Matrix Two
6 8
45   38    5   56   18   34    4   30
87   56   23   41   75   87   97   49
45   97   86    7    6    8   85   77
67    6   79   65   41   37    4   53
 7   76   57   68    8   78    2   14
21   18   46   99   17    3   11   73
Matrix Three
6 6
45   38    5   56   18   34
87   56   23   41   75   87
45   97   86    7    6    8
67    6   79   65   41   37
 7   76   57   68    8   78
21   18   46   99   17    3

Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s)

On the line where I parse words[k] into matrix1[i,j] I get an error message. Parse works fine the first time I use words[] but not the second time I read something in.

1
What is the input file? what error do you get? - L.B
Unhandled Exception: System.FormatException: Input string was not in a correct format. - MDub
In the interest of clarity you should probably put the error message you are getting into the question itself. Also you should clarify if that sample data is the sample output or input? - leigero
How are you not getting an indexOutOfBounds error on your first line 3 when you read that line and then try to set matrix1W to words[1] - leigero
do you have one space between each value in your input file or do they have multiple spaces as shown? - Matthew Whited

1 Answers

-2
votes

The issue is reading the next line from inside of your inner loop. You need to read the line on a row instead of per cell.

var numOfMatrix = int.Parse(fileIn.ReadLine().Trim());
var matrices = new int[numOfMatrix][,];
for (var matrixNumber = 0; matrixNumber < numOfMatrix; matrixNumber++)
{
    var nameOfMatrix1 = fileIn.ReadLine().Trim();
    var words = fileIn.ReadLine().Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    var matrix1H = int.Parse(words[0]);
    var matrix1W = int.Parse(words[1]);

    var matrix1 = matrices[matrixNumber] = new int[matrix1H, matrix1W];            
    // don't use <=
    for (int i = 0; i < matrix1H; i++)
    {
        // read line line outside of the inner loop
        words = fileIn.ReadLine().Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        // don't use <=
        for (int j = 0; j < matrix1W; j++)
        {
            matrix1[i, j] = int.Parse(words[j]);
        }
    }
}