0

.txtファイルからintの配列から2d配列にロードしようとしていますが、「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];
                    }
                }
            }

        }
    }
4

1 に答える 1

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");
}
于 2012-11-20T16:33:23.280 に答える