我怎样才能为每个名字腾出时间

时间:2015-07-04 17:15:04

标签: java arrays

**它告诉我同一时间23,1986年5月输出......人0:John Davidson,23,1986年5月,贝尔格莱德; 人1:迈克尔巴顿,23岁,1986年5月,克拉科夫; 人2:Ivan Perkinson,23岁,1986年5月,莫斯科; **

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class Persons {
    public String name;
  public String lastName;
   public String Birthplace;
   public static  String dateBirth;
     Persons(String name, String lastName, String Birthplace, String dateBirth) {
            this.name = name;
            this.lastName = lastName;
            this.Birthplace = Birthplace;
            Persons.dateBirth = dateBirth;
    }
    public String getName() {
          return name;
   }

    public String getLastName() {
           return lastName;
    }
  public String getPlace() {
            return Birthplace;
  }
    public String getDateBirth() {
            return dateBirth;
  }

   public void setDateBirth(String dateBirth) {
     Persons.dateBirth = dateBirth;
   }

  public static void main(String[] args) throws ParseException {
      SimpleDateFormat formatter = new SimpleDateFormat("dd ',' MMM yyyy", Locale.ENGLISH);
  SimpleDateFormat parser = new SimpleDateFormat("ddMMyyyy");
      List<Persons> details = new ArrayList<Persons>();
      String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow";
      String[] personsSplit = text.split(" ");
      for (int i = 0; i < personsSplit.length; i++) {
      String[]  personList = personsSplit[i].split("/");
      String[] print = personList[0].split("\\.");
      Persons newPerson = new Persons(print[0], print[1],
      personList[2],  personList[1]);
      details .add(newPerson);
           }
          for (int i = 0; i <details .size(); i++) {
                   String outputString = "Person " + i + ": "
                                    + details .get(i).getName() + " "
                                    + details .get(i).getLastName() + ", "
                                    +formatter.format(parser.parse(dateBirth)) + ", "
                                    + details .get(i).getPlace() + ";";
                    System.out.println(outputString);
            }
    }
}

2 个答案:

答案 0 :(得分:2)

你为前三个变量做了正确的事,但后来你决定去一个静态变量(这意味着它在一个Person的所有实例之间共享)。只需将dateBirth声明更改为:

即可
public String dateBirth;

在构造函数中,而不是Persons.dateBirth使其像其他变量一样:

this.dateBirth = dateBirth;

答案 1 :(得分:0)

变量dateBirth是静态的。这意味着无论何时在一个人身上进行更改,它都会在所有其他人身上进行更改。最后一个人的出生日期是1968年5月23日,所以,因为这是变量最后一次改变,所以所有人都有他们的出生日期。

修正:

  1. static移除public static String dateBirth

  2. Person.dateBirth的所有实例更改为this.dateBirth

  3. 要修正错误Cannot make a static reference to the non-static field dateBirth,请将dateBirth方法中的main更改为details.get(i).getDateBirth()

  4. 您的最终代码应为:

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;
    public class Persons {
        public String name;
        public String lastName;
        public String Birthplace;
        public String dateBirth;
        Persons(String name, String lastName, String Birthplace, String dateBirth) {
            this.name = name;
            this.lastName = lastName;
            this.Birthplace = Birthplace;
            this.dateBirth = dateBirth;
        }
        public String getName() {
            return name;
        }
    
        public String getLastName() {
            return lastName;
        }
        public String getPlace() {
            return Birthplace;
        }
        public String getDateBirth() {
            return dateBirth;
        }
    
        public void setDateBirth(String dateBirth) {
            this.dateBirth = dateBirth;
        }
    
        public static void main(String[] args) throws ParseException {
            SimpleDateFormat formatter = new SimpleDateFormat("dd ',' MMM yyyy", Locale.ENGLISH);
            SimpleDateFormat parser = new SimpleDateFormat("ddMMyyyy");
            List<Persons> details = new ArrayList<Persons>();
            String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow";
            String[] personsSplit = text.split(" ");
            for (int i = 0; i < personsSplit.length; i++) {
                String[]  personList = personsSplit[i].split("/");
                String[] print = personList[0].split("\\.");
                Persons newPerson = new Persons(print[0], print[1],
                        personList[2],  personList[1]);
                details .add(newPerson);
            }
            for (int i = 0; i <details .size(); i++) {
                String outputString = "Person " + i + ": "
                        + details .get(i).getName() + " "
                        + details .get(i).getLastName() + ", "
                        +formatter.format(parser.parse(details .get(i).getDateBirth())) + ", "
                        + details .get(i).getPlace() + ";";
                System.out.println(outputString);
            }
        }
    }
    

    希望这有帮助!