-4

I have an array of structs and I keep getting the error code "Input String was not in the correct format" next to:

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

here is the code around it:

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

I have tried a few things, but the error wont go away, does anyone know what I can do? Thank you in advance if anyone can help!

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • 4
    Clearly the input file had a non-number where you expected a number. What was it? Can you show the relevant portion of the file? The value of the variable at exception time in the debugger? – BradleyDotNET Oct 07 '14 at 00:23
  • 1
    Potential duplicate of: http://stackoverflow.com/questions/7532801/system-formatexception-input-string-was-not-in-a-correct-format – BradleyDotNET Oct 07 '14 at 00:35
  • 1
    It'd help if you elaborated on the "few things" you tried. – Grant Winney Oct 07 '14 at 00:52

1 Answers1

0

This exception just means that inFile.ReadLine() doesn't return a string that can be converted to int16. Make sure your file is correct.

ByoTic
  • 103
  • 8