Why are not the words stored in the arrays correctly stored?

1

The objective of this program is that given a sequence of characters entered verify if a word is a palindrome (word that is symmetric from right to left: qwewq qweewq) once verified that it is a palindrome to count the number of letters to see if the word is even or odd. Yes NumberParksPar < NumberImpager Words will only print the odd palindromes and vice versa. In the case that there are the same number of odd and even pairs, all palindromes will be printed (this is still to be developed, do not pay much attention to the final part of the program).

My problem is that it only prints the last word entered, I think the fault is in the arrays. I'm not going to hang up the methods and classes I use, since I'm only interested in knowing if there is a failure when it comes to storing.

public class PalindromosParesImpares{

    static final int MAXIMO_PALABRAS = 500;
    static int contadorP = 0;
    static int contadorI = 0;
    static final int MAXIMO = 20 + 1;
    static Palabra[] palabraspar = new Palabra[MAXIMO_PALABRAS];
    static Palabra[] palabrasim = new Palabra[MAXIMO_PALABRAS];
    static char[] palabra = new char[MAXIMO];
    static int numCaracteres;

    public static void main(String[] args) throws Exception {
        System.out.print("Introduce la secuencia de palabras: ");
        Palabra pal=new Palabra();

        //HAY PALABRAS
        while (Palabra.hayPalabras()) {

            //LEER PALABRAS
            pal.lectura();

            //Es palindromo
            if (pal.esPalindromo()) {

                //Palabra PAR
                if (pal.palabraPar()) {
                    palabraspar[contadorP] = pal;
                    contadorP++;
                }

                //Palabra IMPAR
                if (!pal.palabraPar()) {
                    palabrasim[contadorI] = pal;
                    contadorI++;
                }

            }

        }

        //Si hay mas PARES que IMPARES se imprimiran las palabras pares
        if (contadorI < contadorP) {

            System.out.println("Las palabras palindromos pares son: ");
            for (int indice = 0; indice < contadorP; indice++) {
                System.out.println(palabraspar[indice]);
            }

        }

        //Si hay mas IMPARES que PARES se imprimiran las palabras impares
        if (contadorP < contadorI) {

            System.out.println("Las palabras palindromos impares son: ");
            for (int indice = 0; indice < contadorI; indice++) {
                System.out.println(palabrasim[indice]);
            }

        }
        //Aun por modificar
        if (contadorP == contadorI) {
            System.out.println("No hay palindromos");
        }

    }

}
    
asked by Roggi 22.12.2016 в 22:44
source

1 answer

1

It's a classic ...

I guess in Palabra you read a word and save it.

But in the array you are not saving String . You are saving Palabra . AND ONLY CREATE A WORD INSTANCE .

So you always keep the same object Palabra which, from what I see of the code, only saves the last one.

Thus, in all positions of the array you have the same object, which gives you the result you are seeing.

Solution: create (and use) an instance other than Palabra for each iteration.

    
answered by 22.12.2016 / 23:18
source