Passing a string to an object

0

I need your help, I have a chain like the following

String texto= "[{abc-123,mazda,caba,2016,s,26000};
{xyz-215,nissan,almera,2014,a,18000};{efg-901,mazda,2,2012,m,17000}]";

I need this chain to separate and add all to a ArrayList of an Automobile object that has the following fields of placa , marca , modelo , anio , transmisión and precio respectively.

I have used the substring method within a for to separate it, then I have replaced the unwanted characters and saved it in another string which results in the following.

  abc-123,mazda,caba,2016,s,26000
  xyz-215,nissan,almera,2014,a,18000
  efg-901,mazda,2,2012,m,17000

Even so I can not add this string , where I save it, to my list of car objects.

List<Automovil> lista = new ArrayList<Automovil>();

PSDT: I am new to the forum, I hope you have let me understand, I appreciate your time dedicated to helping me.

    
asked by Eddie Gomez Ochante 21.06.2017 в 06:39
source

2 answers

3

There may be many ways to do this task, for this case I will propose an option. First you have to replace the special characters Excepto which will allow us to separate the records and the attributes that are ; and ,

String texto= "[{abc-123,mazda,caba,2016,s,26000};
               {xyz-215,nissan,almera,2014,a,18000};{efg-901,mazda,2,2012,m,17000}]";
/* Reemplazamos los { , [] y } */
texto = texto.replaceAll("[{\[\]}]", "");

After replacing we proceed to separate the records making use of split , in the variable parts we will have the values that will be passed to the constructor of your automobile class.

for (String partes : texto.split(";")) {
  String[] part  = partes.split(",") ;
  /* Hacemos uso del Constructor , si los tipos de datos sin diferentes 
    solo es cuestión de parsear al tipo correspondiente */
  Automovil movil = new Automovil( part[0], part[1], part[2], Integer.parseInt(part[3]), 
                                    part[4], Float.parseFloat(part[5]));
  /* Añadimos a la Lista el Objeto*/
  lista.add(movil);
}

For this to work in its class Automovil should have a constructor similar to this, if not it should adapt it and make the cast respective when calling the constructor.

 public Automovil(String placa, String marca, String modelo, int anio, String transmision, float precio) {
    this.placa = placa;
    this.marca = marca;
    this.modelo = modelo;
    this.anio = anio;
    this.transmision = transmision;
    this.precio = precio;
}
    
answered by 21.06.2017 / 07:30
source
0

Try this:

String cadena = "abc-123,mazda,caba,2016,s,26000;"
                    +"xyz-215,nissan,almera,2014,a,18000;"
                    + "efg-901,mazda,2,2012,m,17000;";
    ArrayList<Automovil> lista = new ArrayList<Automovil>();

    String split[] = cadena.split(";");
    for (int x = 0; x < split.length; x++) {
        String splitFila[] = split[x].split(",");
        lista.add(new Automovil(splitFila[0],splitFila[1],splitFila[2],splitFila[3],splitFila[4],splitFila[5]));
    }

separates each row by some character, in this case by the semicolon (;).

Then you have a split of the chain, separate the rows by the semicolon and then you have a split of the row when you find a comma (,), this to separate each element, in this case the split of the row will be of size 6.

Now just create a car object and save it to the ArrayList.

    
answered by 21.06.2017 в 07:38