如何通过拆分字符串创建一个 Person 对象

huangapple go评论74阅读模式
英文:

How to create a Person object by splitting a string

问题

public class Main {

    public static void main(String[] args) {
        String warehouse = "John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow";
        String[] peopleArray = warehouse.split(" ");

        Person[] personObjects = new Person[peopleArray.length / 3]; // Assuming each person's data has 3 parts

        for (int i = 0; i < peopleArray.length; i += 3) {
            String[] personData = peopleArray[i].split("/|\\.");
            String name = personData[0];
            String lastname = personData[1];
            LocalDate dateOfBirth = LocalDate.of(Integer.parseInt(personData[2].substring(4)), 
                                                 Integer.parseInt(personData[2].substring(2, 4)),
                                                 Integer.parseInt(personData[2].substring(0, 2)));
            String placeOfBirth = peopleArray[i + 2];

            personObjects[i / 3] = new Person(name, lastname, dateOfBirth, placeOfBirth);
        }

        for (Person person : personObjects) {
            System.out.println(person);
        }
    }
}

(Note: You provided a mix of text and code in Chinese and requested only code translation. The provided code is a direct translation of the given code into functional English code. Make sure to adjust and test the code as needed in your development environment.)

英文:

I have to split a string (John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow) using split method(" ");
I think I should use a for loop to obtain the values corresponding to the field of the Person class, but how do I do that and how do I create a Person object and add it to an object list?
I've done this:

    String[] peopleArray = warehouse.split(&quot; &quot;);
    String[][] personArray = new String[peopleArray.length][4];
    for (int i = 0; i &lt; peopleArray.length; i++) {
        personArray[i] = peopleArray[i].split(&quot;/|\\.&quot;);
        
        for (String field : personArray[i]) {
            
            System.out.print(field + &quot; &quot;);
        }
        System.out.println(&quot; &quot;);

I can't get the fields with this method

Person class is created with the name, lastname, date of birth , and place of birth, fields.

This is my Person Class

package warehouse;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Person {

public String name;

public String lastname;

public LocalDate dateOfBirth;

public String placeOfBirth;

DateTimeFormatter FORM = DateTimeFormatter.ofPattern(&quot;dd.MMM yyyy.&quot;);


public Person(String name, String lastname, LocalDate dateOfBirth, String placeOfBirth) {

    this.name = name;

    this.lastname = lastname;

    this.placeOfBirth = placeOfBirth;

    this.dateOfBirth = dateOfBirth;

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getPlaceOfBirth() {
    return placeOfBirth;
}

public void setPlaceOfBirth(String placeOfBirth) {
    this.placeOfBirth = placeOfBirth;
}

public String getLocation() {
    return dateOfBirth.format(FORM);
}

public void setDateOfBirth(LocalDate dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}
@Override

public String toString() {

    return (&quot;Name: &quot; + name + &quot; ; &quot; + &quot;Lastname: &quot; + lastname + &quot; ; &quot; + &quot;Date of birth: &quot; + dateOfBirth.format(FORM) + &quot; ; &quot;
            + &quot;Place of birth: &quot; + placeOfBirth);

}

}

答案1

得分: 0

public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException, ParseException {
    String warehouse = "John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow";
    String[] peopleArray = warehouse.split(" ");
    List<Person> persons = new ArrayList<>();
    for (int i = 0; i < peopleArray.length; i++) {
        String[] person = peopleArray[i].split("/|\\.");
        SimpleDateFormat format1 = new SimpleDateFormat("ddMMyyyy"); // first example
        Date d1 = format1.parse(person[2]);
        persons.add(new Person(person[0], person[1], d1, person[3]));
    }
}

This may work for below person class:

static class Person {
    String name;
    String lastName;
    Date date;
    String place;

    public Person(String name, String lastName, Date date, String place) {
        this.name = name;
        this.lastName = lastName;
        this.date = date;
        this.place = place;
    }
}
英文:
public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException, ParseException {
    String warehouse = &quot;John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow&quot;;
    String[] peopleArray = warehouse.split(&quot; &quot;);
    List&lt;Person&gt; persons = new ArrayList&lt;&gt;();
    for (int i = 0; i &lt; peopleArray.length; i++) {
        String[] person = peopleArray[i].split(&quot;/|\\.&quot;);
        SimpleDateFormat format1 = new SimpleDateFormat(&quot;ddMMyyyy&quot;); // first example
        Date d1 = format1.parse(person[2]);
        persons.add(new Person(person[0], person[1], d1, person[3]));
    }
}

This may work for below person class:

static class Person {
    String name;
    String lastName;
    Date date;
    String place;

    public Person(String name, String lastName, Date date, String place) {
        this.name = name;
        this.lastName = lastName;
        this.date = date;
        this.place = place;
    }
}

huangapple
  • 本文由 发表于 2020年8月23日 16:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63544730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定