好吧我试图从.txt文件加载到一个2d数组的int数组但是它让我感到困惑 “mscorlib.dll中发生了'System.FormatException'类型的未处理异常” 我相信它与循环有关,也许超出界限。但我对c#很新,所以我很难过。
它打破了“nRow [r] = Convert.ToInt32(row [r]);”
protected void read_lvl()
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
while (!reader.EndOfStream)
{
//for each row
for (int i = 0; i < rows; i++)
{
//read in the line
string myLine = reader.ReadLine();
//take out the commas
string[] row = myLine.Split(',');
//convert to string to ints
int[] nRow = new int[row.Length];
for(int r=0; r<row.Length;r++){
nRow[r] =Convert.ToInt32(row[r]);
}
//feed back into the array
for (int j = 0; j < columns; j++){
myreadArray[i, j] = nRow[j];
}
}
}
}
}
答案 0 :(得分:1)
如果row [r]可能不是整数,则应使用Int32.TryParse。
http://msdn.microsoft.com/en-US/library/vstudio/f02979c7.aspx
使用
int nb = 0;
if (Int32.TryParse("12", out nb) == false)
{
Console.WriteLine("Error");
}