How can I split a string, then use the parts to create an object, and then create an array with these objects?

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

How can I split a string, then use the parts to create an object, and then create an array with these objects?

问题

我需要解决一个有点复杂的任务,但似乎做得不对。

我有以下字符串:

  1. String text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";

简而言之,程序流程应该如下:

  • 将给定的字符串拆分成多个部分以获取名字、姓氏和城市;

  • 使用获取的数据创建Person对象;

  • 将获取的Person对象放入Person[]数组中;

  • 循环遍历数组并打印出其数据。

对于给定的字符串,输出应该如下所示:

  • John Davidson Belgrade

  • Michael Barton Krakow

  • Ivan Perkinson Moscow

这是主要代码:

  1. public class Program {
  2. public static void main(String[] args) {
  3. String text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";
  4. String[] array1 = text.split("[/. ]");
  5. for (String s : array1) {
  6. System.out.println(s);
  7. }
  8. Person[] array = new Person[3];
  9. for (int i=0; i < 3; i++)
  10. {
  11. array[i] = new Person();
  12. }
  13. array[0] = new Person("John", "Davidson","Belgrade");
  14. array[1] = new Person("Michael", "Barton", "Krakow");
  15. array[2] = new Person("Ivan", "Perkinson","Moscow");
  16. for (Person value : array) {
  17. System.out.println(value);
  18. }
  19. }
  20. }

这是Person类:

  1. public class Person {
  2. public String name;
  3. public String surname;
  4. public String city;
  5. public Person(){};
  6. public Person(String name, String surname, String city){
  7. this.name = name;
  8. this.surname = surname;
  9. this.city = city;
  10. }
  11. public String getInfo() {
  12. return "Name: " + this.name + "\n" + "Surname: " + this.surname + "\n" + "City: " + this.city;
  13. }
  14. }

不幸的是,结果是这样的:

John

Davidson

Belgrade

Michael

Barton

Krakow

Ivan

Perkinson

Moscow

Person@5f184fc6

Person@3feba861

Person@5b480cf9

我做错了什么?我甚至不知道最后三个条目是什么...

PS:对于我的糟糕英语,非母语者请谅解。

英文:

I need to solve a somewhat complicated task, but I don't seem to get it right.

I have the following String:

  1. String text = &quot;John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow&quot;;

In short, the program flow should be like this:

  • split the given String into multiple parts to get first name, last name, and cities;

  • use the obtained data to create the Person object;

  • put the obtained Person objects into a Person[] array;

  • loop through an array and print out its data.

On output, the following display should be obtained for the string given as an example:

  • John Davidson Belgrade

  • Michael Barton Krakow

  • Ivan Perkinson Moscow

This is the main code:

  1. public class Program {
  2. public static void main(String[] args) {
  3. String text = &quot;John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow&quot;;
  4. String[] array1 = text.split(&quot;[/. ]&quot;);
  5. for (String s : array1) {
  6. System.out.println(s);
  7. }
  8. Person[] array = new Person[3];
  9. for (int i=0; i &lt; 3; i++)
  10. {
  11. array[i] = new Person();
  12. }
  13. array[0] = new Person(&quot;John&quot;, &quot;Davidson&quot;,&quot;Belgrade&quot;);
  14. array[1] = new Person(&quot;Michael&quot;, &quot;Barton&quot;, &quot;Krakow&quot;);
  15. array[2] = new Person(&quot;Ivan&quot;, &quot;Perkinson&quot;,&quot;Moscow&quot;);
  16. for (Person value : array) {
  17. System.out.println(value);
  18. }
  19. }
  20. }

This is the Person class:

  1. public class Person {
  2. public String name;
  3. public String surname;
  4. public String city;
  5. public Person(){};
  6. public Person(String name, String surname, String city){
  7. this.name = name;
  8. this.surname = surname;
  9. this.city = city;
  10. }
  11. public String getInfo() {
  12. return &quot;Name: &quot; + this.name + &quot;\n&quot; + &quot;Surname: &quot; + this.surname + &quot;\n&quot; + &quot;City: &quot; + this.city;
  13. }
  14. }

Unfortuntaley, the result is this:

John

Davidson

Belgrade

Michael

Barton

Krakow

Ivan

Perkinson

Moscow

Person@5f184fc6

Person@3feba861

Person@5b480cf9

What am I doing wrong? I don't know even what the last three entries are...

PS: Sorry for my bad English, not a native speaker.

答案1

得分: 2

  1. String text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";
  2. String[] names = text.split(" ");
  3. List<String> output = Arrays.stream(names)
  4. .map(x -> x.replaceAll("[./]", " "))
  5. .collect(Collectors.toList());
  6. System.out.println(output);
  7. // [John Davidson Belgrade, Michael Barton Krakow, Ivan Perkinson Moscow]
英文:

Split on space first, the iterate the array of names and do a replacement:

<!-- language: java -->

  1. String text = &quot;John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow&quot;;
  2. String[] names = text.split(&quot; &quot;);
  3. List&lt;String&gt; output = Arrays.stream(names)
  4. .map(x -&gt; x.replaceAll(&quot;[./]&quot;, &quot; &quot;))
  5. .collect(Collectors.toList());
  6. System.out.println(output);
  7. // [John Davidson Belgrade, Michael Barton Krakow, Ivan Perkinson Moscow]

答案2

得分: 1

你试图做的事情是错误的,因为你无法使用固定大小初始化数组,因为你不知道字符串中有多少人,所以最好使用ArrayList。

  1. public static void main(String[] args) {
  2. String text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";
  3. String[] personsString = text.split(" ");
  4. List<Person> persons = new ArrayList<Person>();
  5. for (String ps : personsString) {
  6. String firstName = ps.split("/")[0].split("\\.")[0];
  7. String lastName = ps.split("/")[0].split("\\.")[1];
  8. String city = ps.split("/")[1];
  9. persons.add(new Person(firstName, lastName, city));
  10. }
  11. for (Person person : persons) {
  12. System.out.println(person.getInfo());
  13. }
  14. }
英文:

What are you trying to do was wrong
you cant initialise the array with a fixed size cause you dont know how much persons are present in the string, so you better use an arrayList

  1. public static void main(String[] args) {
  2. String text = &quot;John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow&quot;;
  3. String[] personsString = text.split(&quot; &quot;);
  4. List&lt;Person&gt; persons = new ArrayList&lt;Person&gt;();
  5. for (String ps : personsString) {
  6. String firtname = ps.split(&quot;/&quot;)[0].split(&quot;\\.&quot;)[0];
  7. String lastname = ps.split(&quot;/&quot;)[0].split(&quot;\\.&quot;)[1];
  8. String city = ps.split(&quot;/&quot;)[1];
  9. persons.add(new Person(firtname, lastname, city));
  10. }
  11. for (Person person : persons) {
  12. System.out.println(person.getInfo());
  13. }
  14. }

答案3

得分: 0

以下是翻译好的部分:

如果您想要将前面两个答案合并成一个Person对象列表,可以像这样操作:

  1. public static void main(String[] args) {
  2. // 您的示例输入
  3. String text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";
  4. // 按任意数量的空格进行拆分
  5. String[] personInfos = text.split("\\s+");
  6. // 流式处理结果
  7. List<Person> persons = Arrays.stream(personInfos)
  8. // 拆分字符串以提取信息部分并从中创建Persons
  9. .map(x -> new Person(
  10. // 通过点拆分,第一个元素是姓名
  11. x.split("\\.")[0],
  12. // 通过斜杠拆分上述操作的第二个元素提供姓氏
  13. x.split("\\.")[1].split("/")[0],
  14. // 和城市
  15. x.split("\\.")[1].split("/")[1]
  16. ))
  17. .collect(Collectors.toList());
  18. // 打印每个人的toString()
  19. persons.forEach(System.out::println);
  20. }

假设您按照以下方式重写了Person::toString()...

  1. public class Person {
  2. @Override
  3. public String toString() {
  4. return String.format("%s, %s (%s)", surname, name, city);
  5. }
  6. }

...输出将如下所示:

  1. Davidson, John (Belgrade)
  2. Barton, Michael (Krakow)
  3. Perkinson, Ivan (Moscow)
英文:

I would split on space(s) first, because that operation already separates the values per person.

If you want to combine both of the previous answers to have a list of Person objects, you could do it like this:

  1. public static void main(String[] args) {
  2. // your example input
  3. String text = &quot;John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow&quot;;
  4. // split by arbitrary amount of whitespaces
  5. String[] personInfos = text.split(&quot;\\s+&quot;);
  6. // stream the result
  7. List&lt;Person&gt; persons = Arrays.stream(personInfos)
  8. // and split the Strings to extract the info parts and create Persons from those
  9. .map(x -&gt; new Person(
  10. // split by dot has name as first element
  11. x.split(&quot;\\.&quot;)[0],
  12. // splitting the second element of above operation by slash provides surname
  13. x.split(&quot;\\.&quot;)[1].split(&quot;/&quot;)[0],
  14. // and city
  15. x.split(&quot;\\.&quot;)[1].split(&quot;/&quot;)[1]
  16. )
  17. ).collect(Collectors.toList());
  18. // print each person&#39;s toString()
  19. persons.forEach(System.out::println);
  20. }

Provided you override the Person::toString() as follows…

  1. public class Person {
  2. @Override
  3. public String toString() {
  4. return String.format(&quot;%s, %s (%s)&quot;, surname, name, city);
  5. }
  6. }

… the output will be this:

  1. Davidson, John (Belgrade)
  2. Barton, Michael (Krakow)
  3. Perkinson, Ivan (Moscow)

huangapple
  • 本文由 发表于 2023年8月10日 17:46:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874550.html
匿名

发表评论

匿名网友

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

确定