淘先锋技术网

首页 1 2 3 4 5 6 7

-------------------------java字符串的作业题字符串分解---------------

 定义一个方法,传入字符串格式为(idcard:name:age:gender)的分割形式如

  "120384722312901:kexin:20:1|

  3219212131312:haotao:22:1|

  120121121312:zhangmeiling:21:2"

  将该字符串分解,并将属性赋给定义的Person类的对象上中并输出。

  提示:创建的每个Person对象,每个对象有idcardnameagegender属性

publicclass Person {

    private String idcard;

   

    private String name;

   

    private String age;

   

    private String gender;

 

   public String getIdcard(){

      returnidcard;

   }

 

   publicvoidsetIdcard(String idcard) {

      this.idcard = idcard;

   }

 

   public String getName() {

      returnname;

   }

 

   publicvoidsetName(String name) {

      this.name = name;

   }

 

   public String getAge() {

      returnage;

   }

 

   publicvoidsetAge(String age) {

      this.age = age;

   }

 

   public String getGender() {

      returngender;

   }

 

   publicvoidsetGender(String gender) {

      this.gender = gender;

   }

 

   @Override

   public String toString() {

      return"Person[idcard=" + idcard + ", name=" + name + ",age=" + age + ", gender=" + gender + "]";

   }

  

   

}

-------------------------------------------

publicclass test12 {

      publicstaticvoidmain(String[] args) {

      //定义源字符串

      //Stringstr = "120384722312901:kexin:20:1|3219212131312:haotao:22:1|120121121312:zhangmeiling:21:2";

      Scannersc = newScanner(System.in);

      //获得字符串

      Stringstr = sc.nextLine();

      //判断输入的字符串不是空

      Person[]ps = getPersons(str);

      //打印数组

      printArr(ps);

   

   }

     

      publicstatic Person[]getPersons(String str) {

             Person[] ps = null;

         //判断输入的字符串不是空

      if(str != null&& !("".equals(str.trim()))){

         //根据|来做字符串的分割

         String[]pstrs = str.split("\\|");

         //创建一个person类的数组

         //注意person类数组虽然有长度但是没有地址,初始值为null

         //我们必须new Person的构造器

          ps = new Person[pstrs.length];

         //遍历字符串的数组

         for (inti = 0; i < pstrs.length; i++) {

           //获得每一个person的字符串数据

            StringpersonStr = pstrs[i];

            System.out.println(personStr);

            //把子字符串按":"来做拆分

            //下面的数组中储存的是学号,姓名,年龄,性别

            String[]personAttr = personStr.split(":");

                 //创建person数组对象

             ps[i] = new Person();

            //给学号赋值

            ps[i].setIdcard(personAttr[0]);

            //给姓名赋值

            ps[i].setName(personAttr[1]);

            //给年龄赋值

            ps[i].setAge(personAttr[2]);

            //给性别赋值

            ps[i].setGender(personAttr[3]);

         }

      }

      returnps;

      }

     

      publicstaticvoidprintArr(Person[] ps) {

        for (inti = 0; i < ps.length; i++) {

        System.out.println(ps[i]);

      }

      }

}

 

结果:

120384722312901:kexin:20:1|3219212131312:haotao:22:1|120121121312:zhangmeiling:21:2

120384722312901:kexin:20:1

3219212131312:haotao:22:1

120121121312:zhangmeiling:21:2

Person[idcard=120384722312901, name=kexin, age=20, gender=1]

Person[idcard=3219212131312, name=haotao, age=22, gender=1]

Person[idcard=120121121312, name=zhangmeiling, age=21, gender=2]