java - Null not getting assigned to one of the fields Address.ZipCode -


i have classes

student(having instance variables private int rollno,     string studentname,      int age,   address address) 

and

address (having instance variables string streetname,      string city,  string zipcode ) 

all being private.

the below class populates data studentdetails.txt file below(some fields can missing. missing fields expected replace 0 int , null string).

1,arjun,12,ghandinagar,pune,411020  5,seema,,,,  

null value not getting assinged adress.zipcode field.

    public class studentdatamanager implements datamanager      {           public list<student> populatedata(string filename)           {               list<student> obj=new arraylist<student>();               try                {               file f=new file(filename);               scanner in=new scanner(f);               while(in.hasnext())               {                   string s=in.nextline();                   string []array=s.split(",");                    if(array[0].isempty())                       array[0]="0";                   int rollno=integer.valueof(array[0]);                   if(array[1].isempty())                       array[1]=null;                   string studentname=array[1];                   if(array[2].isempty())                       array[2]="0";                   int age=integer.parseint(array[2]);                    address temp1=new address();                   if(array[3].isempty())                       array[3]=null;                   if(array[4].isempty())                       array[4]=null;                   if(array[5].isempty())                       array[5]=null;                    temp1.setstreetname(array[3]);                   temp1.setcity(array[4]);                    temp1.setzipcode(array[5]);                   student temp2=new student(rollno,studentname,age,temp1);                   obj.add(temp2);               }             }              catch (filenotfoundexception e)             {                 e.printstacktrace();             }               return obj;    } } 

the code class address is:

public class address{     private string streetname;     private string city;     private string zipcode;       public address(string streetname, string city, string zipcode) {         this.streetname=streetname;         this.city=city;         this.zipcode=zipcode;     }     public address() {         // todo auto-generated constructor stub     }     public string getstreetname() {         return streetname;     }     public void setstreetname(string streetname) {         this.streetname = streetname;     }     public string getcity() {         return city;     }     public void setcity(string city) {         this.city = city;     }     public string getzipcode() {         return zipcode;     }     public void setzipcode(string zipcode) {         this.zipcode = zipcode;     }        public string tostring()     {         return city+":"+streetname+":"+zipcode;     } } 

you cannot empty string .split(regex) version of split method.
instead use split(regex, limit) , pass -1 limit.

string []array = s.split(",", -1); 

edit :

i tried using still last field not printed null :( – delfin
input ->5,seema,,,, output expected is: [5:seema:0:null:null:null]

why print null last field. passing text 5,seema,,,, i.e. space after last ,. if want null too. call trim() before isempty()

array[5].trim().isempty() 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -