1

I've been trying to complete an assignment for my game design class in C#, one of the issues I have been having is that I cannot load my Save1.GAME text file with all the loading information, I receive a "System.FormatException: 'Input string was not in a correct format.'" error.

At the moment, this assignment includes a small panel where you, the player designs a Sokoban (https://en.wikipedia.org/wiki/Sokoban) map, I have already done this part and now I must load that save file into my program, then generate the "tiles" (the tiles just being little squares where items are) into the different panel where the game will actually be played.

So far I've tried to load the file and write it into a string array, line by line. I've also tried to write the entire file into a string and use the .split(',') function, to no avail. I've tried quite a few ideas that honestly I've forgotten every single one of them.


My load button on the form where the game will be played:

private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openLevelDialog = new OpenFileDialog();
            openLevelDialog.Title = "Select level to load";



            if (openLevelDialog.ShowDialog() == DialogResult.OK)
            {

                string MyString = System.IO.File.ReadAllText(openLevelDialog.FileName);


                int i = 0;
                int j = 0;

                //My array where I will just dump the whole file into.
                int[,] result = new int[10, 10];

                //foreach loop where I attempt to go line-by-line and split the individual numbers by ','
                foreach (var row in MyString.Split('\n'))
                {
                    j = 0;
                    foreach (var col in row.Trim().Split(','))
                    {
                        result[i, j] = int.Parse(col.Trim()); //Exception happens here.
                        j++;
                    }
                    i++;
                }

                //Just an attempt to display what the variable values in my form, ignore this part.
                for (int a = 0; a < result.GetLength(0); a++)
                {
                    for (int w = 0; w < result.GetLength(1); w++)
                    {
                        label1.Text += result[a,w].ToString();
                    }
                }  
            }
        }

And this is the Game1.GAME file

2,2 <--- This is the size of the map, 2X2 = 4 tiles total. ---

0,0,0 <--- This is tile [0,0] and according to my TileTypes enum inside my tile.cs class it should be empty hence, the third 0. ---

0,1,1 <--- This is tile [0,1] and according to my TileTypes enum inside my tile.cs class it should be "The Hero" hence, the 1. ---

1,0,2 <--- This is tile [1,0] and according to my TileTypes enum inside my tile.cs class it should be a wall hence, the 2. ---

1,1,3 <--- This is tile [1,1] and according to my TileTypes enum inside my tile.cs class it should be a box hence, the 3. ---

note: there is a 4th enum with the value of "Destination" but in this particular map I just didn't add any.

How it looks usually

2,2
0,0,0
0,1,1
1,0,2
1,1,3

I was hoping it would just load the string, chop it up into the int array but I cant seem to get past the exception no matter what I do.

Thanks for you're time in advance.


Here's my file from inside notepad++

My System.IO return

5
  • hmm.. potentially, you have other characters in your file like \r (carrier return) or line feed perhaps? can you open this file up in notepad++ , select "View", "Show All Symbols" and screenshot and display that here? Commented Nov 4, 2019 at 1:02
  • can't reproduce your error using your sample file contents and your handling. have you debugged this, is openLevelDialog.FileName a full path? System.IO.File.ReadAllText returning what you expect? Commented Nov 4, 2019 at 1:19
  • @BrettCaswell, My openLevelDialog.FileName returns a full path, The System.IO command also returns the numbers and commas all perfectly formatted Commented Nov 4, 2019 at 1:22
  • yea.. I suppose the only possible scenario here is that last new line.. you're erroring out on the last row and column because it's empty. Commented Nov 4, 2019 at 1:34
  • it's also worth noting that you can use ReadAllLines instead of ReadAllText, replacing .Split('\n') usage. Commented Nov 4, 2019 at 1:49

1 Answer 1

1

Based on the information you've provided, you're erroring out on the last line because newline at the end of your last record.

One means of handling this is to check whether row is Null, Empty, Whitespace in your iteration, and continue your loop if that condition is true.

foreach (var row in MyString.Split('\n')) 
{
   //skip iteration if row is null, empty, or whitespace
   if (string.IsNullOrWhitespace(row))
      continue;
Sign up to request clarification or add additional context in comments.

Comments

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.