尝试将文件中的 int 加载到二维数组中,收到 "Input string was not in a correct format" 错误
Attempting to load int from file into 2d array, receiving "Input string was not in a correct format" error
我一直在尝试用 C# 完成我的游戏设计 class 的作业,我一直遇到的问题之一是我无法加载我的 Save1.GAME 文本文件加载信息,我收到 "System.FormatException: 'Input string was not in a correct format.'" 错误。
目前,这个任务包括一个小面板,玩家可以在其中设计一个推箱子 (https://en.wikipedia.org/wiki/Sokoban) 地图,我已经完成了这部分,现在我必须将该保存文件加载到我的程序中,然后将 "tiles"(方块只是物品所在的小方块)生成到实际玩游戏的不同面板中。
到目前为止,我已经尝试加载文件并将其逐行写入字符串数组。
我还尝试将整个文件写入一个字符串并使用 .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 <--- 这是地图的大小,2X2 = 总共 4 个图块。 ---
0,0,0 <--- 这是图块 [0,0],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是空的,因此,第三个 0。 ---
0,1,1 <--- 这是图块 [0,1],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是 "The Hero" 因此, 1. ---
1,0,2 <--- 这是瓷砖 [1,0],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是一堵墙,因此,2。 ---
1,1,3 <--- 这是图块 [1,1],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是一个盒子,因此,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
根据您提供的信息,您在最后一行出错,因为换行符位于最后一条记录的末尾。
处理此问题的一种方法是检查 row
是否是 Null
、Empty
、Whitespace
在您的迭代中,以及 continue
您的循环,如果该条件是 true
.
foreach (var row in MyString.Split('\n'))
{
//skip iteration if row is null, empty, or whitespace
if (string.IsNullOrWhitespace(row))
continue;
我一直在尝试用 C# 完成我的游戏设计 class 的作业,我一直遇到的问题之一是我无法加载我的 Save1.GAME 文本文件加载信息,我收到 "System.FormatException: 'Input string was not in a correct format.'" 错误。
目前,这个任务包括一个小面板,玩家可以在其中设计一个推箱子 (https://en.wikipedia.org/wiki/Sokoban) 地图,我已经完成了这部分,现在我必须将该保存文件加载到我的程序中,然后将 "tiles"(方块只是物品所在的小方块)生成到实际玩游戏的不同面板中。
到目前为止,我已经尝试加载文件并将其逐行写入字符串数组。 我还尝试将整个文件写入一个字符串并使用 .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 <--- 这是地图的大小,2X2 = 总共 4 个图块。 ---
0,0,0 <--- 这是图块 [0,0],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是空的,因此,第三个 0。 ---
0,1,1 <--- 这是图块 [0,1],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是 "The Hero" 因此, 1. ---
1,0,2 <--- 这是瓷砖 [1,0],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是一堵墙,因此,2。 ---
1,1,3 <--- 这是图块 [1,1],根据我的 tile.cs class 中的 TileTypes 枚举,它应该是一个盒子,因此,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
根据您提供的信息,您在最后一行出错,因为换行符位于最后一条记录的末尾。
处理此问题的一种方法是检查 row
是否是 Null
、Empty
、Whitespace
在您的迭代中,以及 continue
您的循环,如果该条件是 true
.
foreach (var row in MyString.Split('\n'))
{
//skip iteration if row is null, empty, or whitespace
if (string.IsNullOrWhitespace(row))
continue;