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

时间:2014-10-07 00:22:40

标签: c#

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

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();
}

我尝试了一些东西,但错误不会消失,有谁知道我能做什么?如果有人可以提供帮助,请提前感谢您!

1 个答案:

答案 0 :(得分:0)

此异常仅表示inFile.ReadLine()不返回可转换为int16的字符串。 确保您的文件正确无误。