0
  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.

9
  • What is the input file? what error do you get? Commented Sep 21, 2016 at 18:55
  • Unhandled Exception: System.FormatException: Input string was not in a correct format. Commented Sep 21, 2016 at 19:05
  • 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? Commented Sep 21, 2016 at 19:35
  • 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] Commented Sep 21, 2016 at 19:47
  • do you have one space between each value in your input file or do they have multiple spaces as shown? Commented Sep 21, 2016 at 19:50

1 Answer 1

-2

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]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I see where that could affect things, but I am still getting the same error on the same line when running my program.
How can you be so sure this answer is correct without even knowing what the input data is in the OP's program.
because if he was to check words[k] on his machine when the exception is throw he will see it says Matrix
His input data is provided in the question.
if you are using more than one space you will also want to remove empty entires.

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.