0

In java, I have defined a first array of characters, char[], with a maximum of 4 elements, and a second array of characters that is filled by user input with a maximum of 4 elements.

I want to compare the first element (index[0]) of the first array with each of the elements of the other array, and so on with each element of the first array (which is compared with each of the elements of the other array)

I know I can't use the == operator since it compares references, and it's the same as the equals() method.

The exercise dictates that I can only use loops or the Arrays class and its equals() method, I have tried in various ways applying logic but I left it up to here, I can't get it, any help?

If matches are found, the program must increment a variable for each match. note: both arrays are of type char

for (int i = 1; i <= MAX_INTENTOS; i++) {
            System.out.print("Código " + i + " de 10>> ");
            letrasEntrada = entrada.nextLine();

            if (i == MAX_INTENTOS) {
                perdiste = true;
            }

            for (int j = 0; j < LARGO_CODIGO; j++) {
                letrasAdivinador[j] = letrasEntrada.charAt(j);
            }

            for (int x = 0; x < letrasPensador.length; x++) {
                Arrays.equals();


//                if (letrasAdivinador[x] == letrasPensador[x]) {
//                    b += 2;
//                } else {
//                    if (x + 1 < letrasPensador.length && letrasAdivinador[x] == letrasPensador[x + 1]) {
//                        b += 1;
//                    }
//                    if (x + 2 < letrasPensador.length && letrasAdivinador[x] == letrasPensador[x + 2]) {
//                        b += 1;
//                    }
//                    if (x + 3 < letrasPensador.length && letrasAdivinador[x] == letrasPensador[x + 3]) {
//                        b += 1;
//                    }
//                }
            }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    *"I know I can't use the == operator since it compares references, and it's the same as the equals() method."* If you are referring to comparing `char` types, that's a mistake. Because `char` is one of the [primitives in Java](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), you *would* use `==` to compare two `char`, and `.equals` would not work. – Old Dog Programmer Feb 11 '23 at 03:09
  • Java [Arrays](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html) has two [.equals methods for `char` arrays](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#equals(char%5B%5D,char%5B%5D)). See that one, and the next one down. – Old Dog Programmer Feb 11 '23 at 03:12
  • 1
    Does this answer your question? [How do I use the character's equals() method in Java?](https://stackoverflow.com/questions/52126561/how-do-i-use-the-characters-equals-method-in-java) – Ole V.V. Feb 11 '23 at 06:52
  • 1
    You should show a little more code. I assume entrada is a Scanner Object that wraps System.in and letrasAdivinador and letrasPensador are each char [], but I shouldn't have to assume that. The code in your question should show that. Also, please explain the problems you encountered, where are you having trouble. – Old Dog Programmer Feb 11 '23 at 15:47
  • yes, someone else advised me to compare two char arrays with ==, since the equals() method would not work – Ricardo Aguilar Feb 13 '23 at 02:55
  • 1
    I was able to find a solution to the problem by iterating over two nested loops and using the == operator for char arrays – Ricardo Aguilar Feb 13 '23 at 02:57
  • Also, the link you shared helped me expand my knowledge, thank you very much – Ricardo Aguilar Feb 13 '23 at 03:02

0 Answers0