Divide a string and put it as attributes of my object

-1

I have a Person class like this:

class persona {

    private String dni;
    private String nombre;
    private String apel1;
    private String apel2;
    private String ciudad;
    private String pais;

    public persona(String dni, String nombre, String apel1, String apel2, String ciudad, String pais) {
        this.dni = dni;
        this.nombre = nombre;
        this.apel1 = apel1;
        this.apel2 = apel2;
        this.ciudad = ciudad;
        this.pais = pais;
    }

    /**
     * @return the dni
     */
    public String getDni() {
        return dni;
    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @return the apel1
     */
    public String getApel1() {
        return apel1;
    }

    /**
     * @return the apel2
     */
    public String getApel2() {
        return apel2;
    }

    /**
     * @return the ciudad
     */
    public String getCiudad() {
        return ciudad;
    }

    /**
     * @return the pais
     */
    public String getPais() {
        return pais;
    }

    public void print(){
        System.out.println("DNI: "+getDni());
        System.out.println("Nombre: "+getNombre());
        System.out.println("Apel1: "+getApel1());
        System.out.println("Apel2: "+getApel2());
        System.out.println("Ciudad: "+getCiudad());
        System.out.println("Pais: "+getPais());
        System.out.println("##########################3");
    }
}

And in my test class I have a String with parameters separated by a comma. How can you divide that string and put it as attributes of my object?

public class Prueba {

    public static void main(String[] args) {
        persona p;
        String s = "1234356B,ramon,retrasado,del culo,paris,francia;"
                 + "4567823J,antonio,carrasco,garcia,madrid,españa";

    }
}
    
asked by Selito95 08.07.2017 в 21:43
source

1 answer

2

I guess you're looking for a way to convert your string to Persona objects, a way to do it would be like that.

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String... args) {

        String s = "1234356B,ramon,retrasado,del culo,paris,francia;"
                + "4567823J,antonio,carrasco,garcia,madrid,españa";

        List<Persona> persons = toPersonaList(s);
        for (Persona p : persons) {
            p.print();

        }

    }

    public static List<Persona> toPersonaList(String data) {

        final String SEMICOLON = ";";
        final String COMMA = ",";
        List<Persona> persons = new ArrayList<>();

        String registers[] = data.split(SEMICOLON);

        for (String register : registers) {

            String fileds[] = register.split(COMMA);
            Persona newPerson = new Persona(fileds[0], fileds[1], fileds[2], fileds[3], fileds[4], fileds[5]);

            persons.add(newPerson);
        }
        return persons;

    }
}
    
answered by 08.07.2017 в 22:01