Null没有被分配到其中一个字段Address.ZipCode

时间:2015-09-30 05:53:23

标签: java

我必须上课

Student(having instance variables private int rollno,
    String studentName,
     int age, 
 Address address)

Address (having instance variables String streetName,
     String city,
 String zipCode )

都是私人的。

下面的类填充StudentDetails.txt文件中的数据,如下所示(某些字段可能会丢失。缺少的字段应该替换为0表示int,null表示字符串)。

1,Arjun,12,GhandiNagar,Pune,411020

5,Seema,,,, 

null值未归入Adress.ZipCode字段。

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

类地址的代码是:

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

1 个答案:

答案 0 :(得分:3)

您无法使用.split(regex)版本的split方法获取空字符串 而是使用split(regex, limit)并传递-1作为限制。

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

修改:

  

我尝试使用它但仍然没有打印最后一个字段null :( - Delfin
   对于输入 - > 5,Seema ,,,,输出预期为:[5:Seema:0:null:null:null]

为什么它会为最后一个字段打印null。实际上你正在传递的文字是5,Seema,,,,,即最后一个空格,。如果你想为此获得null。而不是在isEmpty()之前调用trim()

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