c# - I cannot figure out how to fix an unhandled Format Excpetion -


i trying write c# program using visual studio reads data file , allow me perform various sorts on data. beginner took research figure out how write code: a:

format exception unhandled. input string not in correct format

i not sure went wrong. happening following line of code:

candidate newrec = new candidate(str[0], str[1], str [2], str[3], str[4], str[5], str[6], convert.toint32(str[7]), str[8], str[9], str[10], str[11]);

the entire code follows:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.io; using system.collections;    namespace unit4ip {    //used sort in asceding , descending order     public struct candidate:icomparable      {         public char[] _firstname;         public char[] _lastname;         public char[] _company;         public char[] _address;         public char[] _city;         public char[] _country;         public char[] _state;         public char[] _phone;         public char[] _fax;         public char[] _email;         public char[] _web;         public int _zip;          //for comparing objects         public int compareto(object obj)         {             candidate candidate2 = (candidate)obj;             return _lastname.tostring().compareto(candidate2._lastname.tostring());         }         //implements sorting based on assignments such zip, lastname, etc.          public int compareto(candidate candidate2,             candidatecomparer.comparisontype comptype)         {             if(comptype==candidatecomparer.comparisontype.lastname)             {                     string _lname = new string(_lastname);                     string lname = new string(candidate2._lastname);                     return _lname.compareto(lname);// convert character array string because compareto works efficiently strings             }             else             {                     return candidate2._zip.compareto(_zip); // compareto values interchanged in descending order             }         }                //constructor of candidate structure          public candidate(string firstname, string lastname, string company, string address, string city, string country, string state, int zip, string phone, string fax, string email, string web)         {             _firstname = new char[12];             _lastname = new char[16];             _company = new char[32];             _address = new char[32];             _city = new char[24];             _country = new char[24];             _state = new char[2];             _phone = new char[12];             _fax = new char[12];             _email = new char[32];             _web = new char[42];             _firstname = firstname.tochararray();             _lastname = lastname.tochararray();             _company = company.tochararray();             _address = address.tochararray();             _city = city.tochararray();             _country = country.tochararray();             _state = state.tochararray();             _zip = zip;             _phone = phone.tochararray();             _fax = fax.tochararray();             _email = email.tochararray();             _web = web.tochararray();         }           //implement icomparer interface nested structure          public struct candidatecomparer : icomparer          {              public enum comparisontype              { lastname = 1, zip = 2 }              private comparisontype _comparisontype;              public comparisontype comptype              {                  { return _comparisontype; }                  set { _comparisontype = value; }              }              public int compare(object x, object y)              {                  candidate candidate1 = (candidate)x;                  candidate candidate2 = (candidate)y;                   return candidate1.compareto(candidate2, _comparisontype);              }          }     } class program     {         static void main(string[] args)         {             arraylist arraytest = new arraylist();             //loading of file 'itco321_u4_sample_data.csv' arralist. file holds values, no heading i.e. remove headings             streamreader stream1 = file.opentext("c:\\users\\cdhss\\documents\\itco321_u4ip_sample_data-2.csv");             string recdata = null;             while ((recdata = stream1.readline()) != null)             {                 string[] str = recdata.split(',');                 candidate newrec = new candidate(str[0], str[1], str [2], str[3], str[4], str[5], str[6], convert.toint32(str[7]), str[8], str[9], str[10], str[11]);                 arraytest.add(newrec);//add struct object arraylist             }                         //traversing of records             console.writeline("traversing records");             foreach (candidate candidate1 in arraytest)             {                 string fname = new string(candidate1._firstname);                 string lname=new string(candidate1._lastname);                 string company = new string(candidate1._company);                 string address=new string(candidate1._address);                 string city=new string(candidate1._city);                 string country = new string(candidate1._country);                 string phone = new string(candidate1._phone);                 string fax = new string(candidate1._fax);                 string email=new string(candidate1._email);                 string web = new string(candidate1._web);                 console.writeline( fname + "," + lname + "," + company + "," + address  + "," + city + "," + country + "," + candidate1._zip + "," + phone + "," + fax + "," + email + "," + web);             }             candidate.candidatecomparer comparer = new candidate.candidatecomparer();             //sort lastname in ascending order             comparer.comptype = candidate.candidatecomparer.comparisontype.lastname;             arraytest.sort(comparer);             console.writeline("sorting of elements lastname");             foreach (candidate candidate1 in arraytest)             {                 string fname = new string(candidate1._firstname);                 string lname = new string(candidate1._lastname);                 string company = new string(candidate1._company);                 console.writeline("\t" + fname + "," + lname + "," + company);             }             // data sorted in desending order of zip field             comparer.comptype = candidate.candidatecomparer.comparisontype.zip;             arraytest.sort(comparer);             console.writeline("sorting of elements zip");             foreach (candidate candidate1 in arraytest)             {                 string fname = new string(candidate1._firstname);                 string lname = new string(candidate1._lastname);                 string company = new string(candidate1._company);                 console.writeline("\t" + fname + "," + lname + "," + company + "," + candidate1._zip);             }              //display records of 'ny' state             console.writeline("display records of ny state");             foreach (candidate candidate1 in arraytest)             {                 string fname = new string(candidate1._firstname);                 string lname = new string(candidate1._lastname);                 string company = new string(candidate1._company);                 string address = new string(candidate1._address);                 string city = new string(candidate1._city);                 string country = new string(candidate1._country);                 string phone = new string(candidate1._phone);                 string fax = new string(candidate1._fax);                 string email = new string(candidate1._email);                 string web = new string(candidate1._web);                 if (new string(candidate1._state).contains("ny"))                     console.writeline(fname + "," + lname + "," + company + "," + address + "," + city + "," + country + "," + candidate1._zip + "," + phone + "," + fax + "," + email + "," + web);             }             console.read();         }     } } 

your problem in call convert.toint32. whatever string passing can't parsed int.

a solution here might use structured exception handling, either try catch blocks or int32.tryparse(string) example this

int intvalue; if (int32.tryparse(str[7], out intvalue))  {      //do thing int value } else {      throw new exception("some informative error message, using string.format include string tried parse"); } 

tryparse returns true if parse successfull, false if not, , puts parsed integer value in second out parameter.

a few other comments on code out.

  1. no 1 uses arraylists in c# anymore. generics introduced long time ago, , better, standpoint of both type safety , speed use list (look c# generics if don't know are).
  2. you don't need copy candidate properties in foreach loop new variables.
  3. give more informative variable names. readability single important quality in code. fname should firstname, recdata should recieveddata. key strokes cheap in world tab completion.

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -