2012-11-20 60 views
0

ok嗡嗡聲試圖從一個.txt文件加載到一個二維數組的整數數組中,但它向我發送 「未處理的類型爲'System.FormatException'的異常發生在mscorlib.dll「 我相信它與循環有關並可能超出範圍。但即時通訊新的C#所以即時難倒。xna 4.0錯誤從字符串[]轉換爲int []

它打破了「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]; 
        } 
       } 
      } 

     } 
    } 
+3

你行[R],是不是能夠被轉換爲數字得到了一些數據。我猜想這是文件結尾或空字符串,但很難說清楚。 – Shaded

+2

在轉換爲打印'row [r]'之前做一些類型的調試輸出並查看它在失敗之前得到的結果。 – Shaded

回答