2016-07-24 25 views
0

我有一套相當長的代碼,用於排序氣候數據。不過,我目前正面臨着其中的代碼將引發System.FormatException的問題給我的錯誤解釋:索引的System.FormatException問題

型「System.FormatException」未處理的異常發生在 mscorlib.dll中

更多信息:索引(從零開始)必須大於或等於零且小於參數列表的大小。

有誰能告訴我爲什麼我得到這個錯誤?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace Climate_Sorting_Application_2._0 
{ 
    public class ClimateRecord 
    { 
     public DateTime RecordDate { get; set; } 
     public int StationId { get; set; } 
     public double RainFall { get; set; } 
     public double Sun { get; set; } 
     public double TMax { get; set; } 
     public double TMin { get; set; } 
     public double AF { get; set; } 

     public override string ToString() 
     { 
      return String.Format("{0,-15:MMMM yyyy}{1,4}{2,8:F4}{3,8:F4}{4,8:F4}{5,8:F4}{6,8:F4}{7,8:F4}", 
       RecordDate, StationId, AF, RainFall, Sun, TMax, TMin); 
     } 
    } 

    public class Program 
    { 

     static IList<ClimateRecord> LoadData() 
     { 
      var result = new List<ClimateRecord>(); 

      using (var monthRdr = new StreamReader("Month.txt")) 
      using (var yearRdr = new StreamReader("Year.txt")) 
      using (var afRdr1 = new StreamReader("WS1_AF.txt")) 
      using (var rainRdr1 = new StreamReader("WS1_Rain.txt")) 
      using (var sunRdr1 = new StreamReader("WS1_Sun.txt")) 
      using (var tmaxRdr1 = new StreamReader("WS1_TMax.txt")) 
      using (var tminRdr1 = new StreamReader("WS1_TMin.txt")) 
      using (var afRdr2 = new StreamReader("WS2_AF.txt")) 
      using (var rainRdr2 = new StreamReader("WS2_Rain.txt")) 
      using (var sunRdr2 = new StreamReader("WS2_Sun.txt")) 
      using (var tmaxRdr2 = new StreamReader("WS2_TMax.txt")) 
      using (var tminRdr2 = new StreamReader("WS2_TMin.txt")) 
      { 
       string year = yearRdr.ReadLine(); 
       while (year != null) 
       { 
        var recordDate = DateTime.ParseExact(year + " " + monthRdr.ReadLine() + " 01", "yyyy MMMM dd", null); 

         var ws1 = new ClimateRecord() 
         { 
          RecordDate = recordDate, 
          StationId = 1, 
          AF = double.Parse(afRdr1.ReadLine()), 
          Sun = double.Parse(sunRdr1.ReadLine()), 
          RainFall = double.Parse(rainRdr1.ReadLine()), 
          TMax = double.Parse(tmaxRdr1.ReadLine()), 
          TMin = double.Parse(tminRdr1.ReadLine()) 
         }; 
        var ws2 = new ClimateRecord() 
        { 
         RecordDate = recordDate, 
         StationId = 2, 
         AF = double.Parse(afRdr2.ReadLine()), 
         Sun = double.Parse(sunRdr2.ReadLine()), 
         RainFall = double.Parse(rainRdr2.ReadLine()), 
         TMax = double.Parse(tmaxRdr2.ReadLine()), 
         TMin = double.Parse(tminRdr2.ReadLine()) 
        }; 
        result.Add(ws1); 
        result.Add(ws2); 
        year = yearRdr.ReadLine(); 
       } 
      } 
      return result; 
     } 
static void PrintData(IEnumerable<ClimateRecord> data) 
    { 
     Console.WriteLine("{0,15}{1,4}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}", 
      "Month/Year", "WS", "AF", "Rain", "Sun", "T-Max", "T-Min"); 

     foreach (var record in data) Console.WriteLine(record); 
    } 

上面的部分是在異常被調用。特別是Console.WriteLine部分。

 static void Main(string[] args) 
     { 
      var climateData = LoadData(); 

      Console.WriteLine("Printing all data: "); 
      PrintData(climateData); 

      Console.WriteLine("\n\nPrinting Station 1 data:"); 
      PrintData(climateData.Where(r => r.StationId == 1)); 

      Console.WriteLine("\n\nPrinting Station 2 data:"); 
      PrintData(climateData.Where(r => r.StationId == 2)); 

      Console.WriteLine("\n\nPrinting Station 1 data ordered by rainfall descending:"); 
      PrintData(climateData.Where(r => r.StationId == 1).OrderBy(r => r.RainFall * -1)); 
     } 
    } 
} 
+0

您的格式串8場,但只有7被填充。 –

回答

2

東西丟失,請參閱???:

Console.WriteLine("{0,15}{1,4}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}", 
      "Month/Year", "WS", "AF", "Rain", "Sun", "T-Max", "T-Min", "???"); 
+0

啊對了。似乎無法弄清楚爲什麼我這樣做 – GiggyLapisar

+0

是的,它沒有任何附加。我認爲它是由於編碼中早先分開的月/年事物而增加的 – GiggyLapisar