【问题标题】:Attempting to load int from file into 2d array, receiving "Input string was not in a correct format" error尝试将 int 从文件加载到二维数组中,收到“输入字符串格式不正确”错误
【发布时间】:2020-02-29 07:48:59
【问题描述】:

我一直在尝试用 C# 完成我的游戏设计课程的作业,我遇到的一个问题是我无法加载包含所有加载信息的 Save1.GAME 文本文件,我收到“系统.FormatException: '输入字符串的格式不正确。'" 错误。

目前,这个任务包括一个小面板,玩家在其中设计一个推箱子 (https://en.wikipedia.org/wiki/Sokoban) 地图,我已经完成了这部分,现在我必须将该保存文件加载到我的程序中,然后生成“瓷砖”(瓷砖只是物品所在的小方块)到实际进行游戏的不同面板中。

到目前为止,我已尝试加载文件并将其逐行写入字符串数组。 我还尝试将整个文件写入字符串并使用 .split(',') 函数,但无济于事。 我尝试了很多想法,老实说,我已经忘记了其中的每一个。


我在将要玩游戏的表单上的加载按钮:

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();
                    }
                }  
            }
        }

这是 Game1.GAME 文件

2,2

0,0,0

0,1,1

1,0,2

1,1,3

注意:有一个值为“Destination”的第 4 个枚举,但在这个特定的地图中,我没有添加任何枚举。

通常的样子

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

我希望它只是加载字符串,将其切碎到 int 数组中,但无论我做什么,我似乎都无法通过异常。

感谢您提前提供时间。


Here's my file from inside notepad++

My System.IO return

【问题讨论】:

  • hmm.. 您的文件中可能还有其他字符,例如 \r(运营商返回)或换行符?你能在notepad++中打开这个文件,选择“查看”、“显示所有符号”并截图并显示在这里吗?
  • 无法使用您的示例文件内容和您的处理方式重现您的错误。你调试过这个,openLevelDialog.FileName 是完整路径吗? System.IO.File.ReadAllText 返回你所期望的?
  • @BrettCaswell,我的openLevelDialog.FileName 返回完整路径,System.IO 命令还返回格式完美的数字和逗号
  • 是的..我想这里唯一可能的情况是最后一行..你在最后一行和最后一列出错了,因为它是空的。
  • 另外值得注意的是,您可以使用ReadAllLines 代替ReadAllText,替换.Split('\n') 用法。

标签: c# arrays forms visual-studio panel


【解决方案1】:

根据您提供的信息,您在最后一行出错,因为在最后一条记录的末尾有换行符。

处理此问题的一种方法是检查您的迭代中row 是否为NullEmptyWhitespace,如果该条件为true,则检查您的循环是否为continue

foreach (var row in MyString.Split('\n')) 
{
   //skip iteration if row is null, empty, or whitespace
   if (string.IsNullOrWhitespace(row))
      continue;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2020-09-02
    • 1970-01-01
    相关资源
    最近更新 更多