Group 4 in 4 in Java

0

I want to group the elements from 4 to 4 in java.

Each question has 4 sub-questions, so those 4 sub-questions belong to you.

                 String[] preguntas = request.getParameterValues("pregunta[]");
                 String[] subpreguntas = request.getParameterValues("subpregunta[]");

                int i = 0;

                //Recorro todas las sub preguntas.
                for (int j = 0; j < subpreguntas.length; j++) {

                    //Cada pregunta tiene 4 subpreguntas, por tanto hay que agrupar.
                    if (j % 4 == 0) {
                        co.Insertar_Sub_pregunta(i, subpreguntas[j]);

                    } else {
                        co.Insertar_pregunta(i, preguntas[i]);
                        i++;
                    }

                }

Insert the questions well, but do not sub-questions, besides I do not have the id of questions in 3, can you help me to group in 4?

    
asked by EduBw 11.03.2018 в 23:08
source

1 answer

1

I think I know where the error is. In if(j % 4 == 0){ you will only enter when j is a multiple of 4 (0, 4, 8 ...), that is, only 1 subquery will be added to each group of 4. Instead, you should add a question every 4 sub-questions:

int i = -1; //Empiezo en -1 para que en la primera iteración se ponga a 0.
//Recorro todas las sub preguntas.
for(int j = 0; j < subpreguntas.length; j++){
    //Cada pregunta tiene 4 subpreguntas, por tanto hay que agrupar.
    if(j % 4 == 0){
        i++;
        co.Insertar_pregunta(i, preguntas[i]);//Se añade la pregunta.
    }//No se necesita else
    co.Insertar_Sub_pregunta(i, subpreguntas[j]);
}

I do not know the code of the object co . In this case, first add 1 question and then your 4 answers. If it works otherwise, make the changes you find appropriate.

    
answered by 11.03.2018 / 23:25
source