java
Properties file and array
Good afternoon, I have a problem. When reading a property file, and passing it to an array, it looks like the array is repeated until you finish reading the document, as shown in the image I'm supposed to read the properties from a txt file that contains them and pass the artist's name to an array of size 10. Here the methods used by the program /** * Carga la información inicial del karaoke. */ private void cargarKaraoke() { try { Properties datos = new Properties(); FileInputStream in = new FileInputStream(RUTA_ARCHIVO); datos.load(in); in.close(); int numArtistas = Integer.parseInt(datos.getProperty("total.artistas")); for(int i = 1; i <= numArtistas; i++) { String nombre = datos.getProperty("artista" + i + ".nombre"); String categoria = datos.getProperty("artista" + i + ".categoria"); String imagen = datos.getProperty("artista" + i + ".imagen"); karaoke.agregarArtista(nombre, categoria, imagen); int numCanciones = Integer.parseInt(datos.getProperty("artista" + i + ".total.canciones")); for(int j = 1; j <= numCanciones; j++) { String cancion = datos.getProperty("artista" + i + ".cancion" + j + ".nombre"); int duracion = Integer.parseInt(datos.getProperty("artista" + i + ".cancion" + j + ".duracion")); String letra = datos.getProperty("artista" + i + ".cancion" + j + ".letra"); int dificultad = Integer.parseInt(datos.getProperty( "artista" + i + ".cancion" + j + ".dificultad")); String genero = datos.getProperty("artista" + i + ".cancion" + j + ".genero"); String ruta = datos.getProperty("artista" + i + ".cancion" + j + ".ruta"); karaoke.agregarCancion(nombre, cancion, duracion, letra, dificultad, genero, ruta); } } } catch(Exception e) { JOptionPane.showMessageDialog(this, "No fue posible cargar la información " + "inicial del karaoke " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } public void agregarArtista(String nombreArtista, String categoria, String imagen) { for (int i = 0; i < artistas.length; i++) { artistas[i] = new Artista(nombreArtista, categoria, imagen); } System.out.println(Arrays.toString(artistas)); } public int agregarCancion(String nombre, int duracion, String letra, int dificultad, String genero, String ruta) { canciones.add(new Cancion(dificultad, duracion, genero, nombre, letra, ruta)); return 1; } But at the time of testing the results of the image appear. Personally I think the problem is in the method to add artist, but I can not identify the problem. Does anyone have any idea what's going on?
You can save all Artista obejcts in a List List<Artista> artistas = new ArrayList<Artista>() and change the Method agregarArtista to fill the list with the Artists Data: public void agregarArtista(String nombreArtista, String categoria, String imagen) { artistas.add(new Artista(nombreArtista, categoria, imagen)); System.out.println(artistas); } If You want only first 10 Artists then You could change the Method as below: public void agregarArtista(String nombreArtista, String categoria, String imagen) { if (artistas.size() < 10) { artistas.add(new Artista(nombreArtista, categoria, imagen)); } System.out.println(artistas); } If the result should be in a Array then You could convert List to an Array like this: Artista[] artistasArray = artistas.toArray(new Artista[0]);
Related Links
Error while updating SQLite Database
JavaFX: How to invalidate an Observable object that contains a list of other Observable objects
ListView not showing on my app
Unit Test Spring MVC Rest Service: array jsonPath
Navigate to folder inside src
Merge Two Sorted Lists, but my head is not updated - Java
how to find second largest index of arraylist in java
how to save scrollpane content as a jpg file
What did I miss in my code to cause Attribute value must be constant
How to mock MyBatis mapper interface?
Hibernate criteria when no mapping exists [duplicate]
XML Schema Augmentation
How to ensure that couchbase utilize a network link asynchronous
What does it mean to open a RandomAccessFile in Java?
Conflict with Spring Framework and and Apache CXF dependencies - 'No Such Method'
Atomicity of Reads and Writes to Variables