索引出现System.FormatException问题
本文关键字:FormatException 问题 System 索引 | 更新日期: 2023-09-27 17:58:19
我有一组相当长的代码,用于对气候数据进行排序。然而,我目前面临的问题是,代码抛出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));
}
}
}
缺少一些东西,请参阅??:
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", "???");