Constructor using String

1

I have to make a constructor in the class CancionImpl that receives a string with the values of the properties separated by semicolons (;), in the following order: type id String , artist type Artist, duration in seconds, type name String , popularity type Integer . I do not know how to get artist or duration.

public CancionImpl(String s) {
    String[] trozos = s.split(";");
    ...
    ...
}   
    
asked by Francisco Solano 29.05.2018 в 17:37
source

3 answers

2

If you use split, you will get an array of elements.

String[] trozos = s.split(";");

the elements can be obtained by their index starting at 0

0 - id de tipo String
1 - artista de tipo Artista, NO se puede obtener un tipo artista ya que los elementos son de tipo String.
2 - duración en segundos
3 - nombre de tipo String
4 - popularidad de tipo Integer.

in this way, to convert to integer you can use Integer.paserInt() :

String id = trozos[0];
String artista = trozos[1];
String duracion = trozos[2];
String nombre = trozos[3];
int popularidad = Integer.paserInt(trozos[4]);
    
answered by 29.05.2018 в 17:49
1

For Artist, send only the String attribute that has the value you want, instead of sending the whole object.

For duration, you only need to convert from String to Integer ... Integer durationInteger = Integer.parseInt (duracionstring)

    
answered by 29.05.2018 в 21:17
0

In principle I see it well you would need a for to remove the values from the array of pieces.

for(int i = 0; i< trozos.length; i++)
System.out.println(trozos[i]);
}

Once you have the list you can parse the values at your whim as long as all the strings you receive have the same order

    
answered by 29.05.2018 в 17:46