英文:
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(" ");
String[][] personArray = new String[peopleArray.length][4];
for (int i = 0; i < peopleArray.length; i++) {
personArray[i] = peopleArray[i].split("/|\\.");
for (String field : personArray[i]) {
System.out.print(field + " ");
}
System.out.println(" ");
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("dd.MMM yyyy.");
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 ("Name: " + name + " ; " + "Lastname: " + lastname + " ; " + "Date of birth: " + dateOfBirth.format(FORM) + " ; "
+ "Place of birth: " + 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 = "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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论