How can I get the contents of an ArrayListString and put them in a constructor?

0

Good evening, my question is how can I get the data that contains an ArrayList and when I get it I can insert it in a parameterized constructor of 4 parameters that are String, String, String, int ?. The class is Magazine with those 4 parameters, and inside the ArrayList I have the data that I have taken from a file, because when I have the data in the arrayList what I want every 4 positions of the array to create a new Journal with its data passing it through Parameters, but always I only get one. This is my code: '

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class PruebaLeerFicheroNuevo {
    private static File fichero = new File("./fichero");
    private static FileReader fr;
    private static BufferedReader br;
    private static ArrayList<Publicacion> publicaciones = new ArrayList<Publicacion>();
    public static Publicacion nueva Publicacion;

public static void main(String[] args) {
    try {
        fr = new FileReader(fichero);
        br = new BufferedReader(fr);
        String palabra;
        ArrayList<String> centinela = new ArrayList<String>();

        while ((palabra = br.readLine()) != null) {
            centinela.add(palabra);

        }

        // System.out.println("DATOS DE CENTINELA: " + centinela.toString());

        ArrayList<Publicacion> mo = new ArrayList<Publicacion>();

        for (int i = 4; i < centinela.size(); i++) {
            String nombre = centinela.get(0);
            String autor = centinela.get(1);
            String materia = centinela.get(2);
            int numero = Integer.parseInt(centinela.get(3));
            nuevaPublicacion = new Revista(nombre, autor, materia, numero);
            publicaciones.add(nuevaPublicacion);
        }

        br.close();

        // MOSTRAMOS LOS DATOS DE publicaciones

        for (int i = 0; i < publicaciones.size(); i++) {
            Publicacion ver = publicaciones.get(i);
            if (ver instanceof Revista) {
                System.out.println("DATOS ver: ");
                System.out.println(ver.toString());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

} '

    
asked by Samuel Tena 20.01.2018 в 22:43
source

1 answer

2

Short solution

 for (int i = 0; i < centinela.size() -4; i+=4) {
        String nombre = centinela.get(i);
        String autor = centinela.get(i+1);
        String materia = centinela.get(i+2);
        int numero = Integer.parseInt(centinela.get(i+3));
        nuevaPublicacion = new Revista(nombre, autor, materia, numero);
        publicaciones.add(nuevaPublicacion);
    }

Let me explain

One of the mistakes you make is that you are initializing the variable i in 4, so the for begins to iterate from position 4 in the list and since there is the value of i is increasing 1 , so that for works in the following way (literal): [4, 5, 6, 7, 8] ... until you reach the end of the list.

Another error that you make is that in all the iterations of the for you get the same values from the list, that's because the method get of the list is passing you static positions: [0, 1, 2, 3 ], so in all iterations you get the values that are in those positions in the list. To go through the list you have to use the value of i , which is increasing in each iteration of the cycle: centinela.get(i) .

To achieve what you want you have to go through the list of 4 in 4 and in each iteration get the values of four positions in the list, that you do with i , to get the value of position 1 in the list use i , to get the value of the second position in the list you use i+1 and so on with all the positions you want to get from the list in each iteration. If you do it that way in the first iteration you will get the values stored in the positions [0, 1, 2, 3]; in the second [4, 5, 6, 7] ...

Keep in mind that when you go through the list of four in four, you have to subtract four from the list size, so that in the last iteration the ArrayIndexOutOfBoundsException exception does not occur. This is because the value of i increases four in each iteration and in each iteration you get 4 list values, taking as an example a list of 12 positions, in the first iteration the value of i is 0 and you go through the positions [ 0, 1, 2, 3], in the second is 4 [4, 5, 6, 7], in the third 8 [8, 9, 10, 11] and in the fourth is 12, but in the penultimate iteration and You go through the last positions of the list and if you keep iterating you will go through positions that do not exist, which will cause the exception.

    
answered by 21.01.2018 / 01:56
source