输入字符串的格式不正确-结构数组

本文关键字:结构 数组 不正确 格式 字符串 输入 | 更新日期: 2023-09-27 18:25:54

我有一个结构数组,我一直在旁边得到错误代码"输入字符串格式不正确"

specification[counter].admissions2010 = 
Convert.ToInt16(inFile.ReadLine()); 

这是它的代码:

static void Main(string[] args)
{
    const int NUMBER_UNIVERSITIES = 37;
    const string FILE_NAME = "undergraduateapplicationsoffersandacceptances2013appendices.txt";
    Universities[] specification = new 
        Universities[NUMBER_UNIVERSITIES];
    StreamReader inFile = new StreamReader(FILE_NAME);
    //input load data into customers array
    for(int counter = 0; counter < NUMBER_UNIVERSITIES; 
        counter++)
    {
        specification[counter].universityName = 
            inFile.ReadLine();
        specification[counter].universityState = 
            inFile.ReadLine();
        specification[counter].admissions2010 = 
            Convert.ToInt16(inFile.ReadLine()); 
        specification[counter].admissions2011 = 
            Convert.ToInt16(inFile.ReadLine());
        specification [counter].admissions2012 = 
            Convert.ToInt16 (inFile.ReadLine());
        specification [counter].admissions2013 =
            Convert.ToInt16(inFile.ReadLine());
    }
    inFile.Close(); //don't forget to close the file :)
    DisplayUniversities(specification);
    Console.ReadKey();
}

我试过几件事,但错误不会消失,有人知道我能做什么吗?如果有人能帮忙,请提前感谢!

输入字符串的格式不正确-结构数组

这个异常只是意味着inFile.ReadLine()不会返回可以转换为int16的字符串。请确保您的文件是正确的。