Raul
Raul

Reputation: 141

IndexOutOfBounds, when it should not

I am learning the Java language, and I am trying to make the MasterMind game. I get the following error while trying to compile, and after trying to debug the code, I cant find the mistake:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 97, Size: 4
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.remove(ArrayList.java:474)
    at mastermind.Combinacion.aciertos(Combinacion.java:115)
    at mastermind.Combinacion.principal(Combinacion.java:36)
    at mastermind.MasterMind.main(MasterMind.java:21)
Java Result: 1

The following code is the method "aciertos", that given a secret key, and a combination, tells you how many of your letters are in the secret key.

    public int aciertos(Combinacion comb){
    int aciert = 0;
    List<Character> clave = new ArrayList();
    // Transform the key to a List, so we can easily check what contains and remove elements
    for(char c : this.codigo){
        clave.add(c);
    }
    // We go though the user try, and we check if the letter it is in the secret key list. If it is, we remove it from the list, to avoid counting twice if the user has a letter repeated and it is only once in the secret key.
    for (int i = 0; i < comb.codigo.length; i++) {
        if(clave.contains(comb.codigo[i])){
            aciert++;
            clave.remove(comb.codigo[i]);
        }
    }
    return aciert;
}

These are the fields from the Combinacion class:

//Size of each combination
private int tamano = 4;
//Los valores válidos son: blanco, negro, azul, rojo, verde, marron
private static Character[] valoresValidos = {'b', 'n', 'a', 'r', 'v', 'm'};
private static List<Character> valores = Arrays.asList(valoresValidos);
private char[] codigo = new char[tamano];

P.D: I should start writing and commenting everything in English, sorry for the spanish words.

Upvotes: 0

Views: 268

Answers (3)

dmolony
dmolony

Reputation: 1135

It would appear that the clave.remove() line is using the contents of comb.codigo[i] as an index. The value being used in this case is 97, which corresponds to a lowercase 'a'. Is that what the array contains?

Upvotes: 0

clstrfsck
clstrfsck

Reputation: 14847

I think the char argument to clave.remove(com.codigo[i]) is being promoted to an int instead of being boxed to a Character. This ends up calling ArrayList#remove(int index) rather than the method that you want (ArrayList#remove(Object o)).

Try manually boxing the char like this:

for (int i = 0; i < comb.codigo.length; i++) {
    if(clave.contains(comb.codigo[i])){
        aciert++;
        clave.remove(Character.valueOf(comb.codigo[i]));
    }
}

Upvotes: 2

alcfeoh
alcfeoh

Reputation: 2267

The error says on its first line that you're trying to read index 97 in an ArrayList that has just 4 elements. ArrayList.remove() uses an index as a parameter, not the object to remove:

clave.remove(comb.codigo[i])

has to be replaced by:

clave.remove(clave.indexOf(comb.codigo[i]))

Upvotes: 6

Related Questions