lundi 29 juin 2015

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

My problem is that I keep getting the error in the title during debugging.

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

The following is the for loop. I entered values people = 10, payer = 4, rest = 6.

int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;

for(int i=0; i<people; i++) {
    if(payer<=0) { /*Payers are already decided*/
        array[i] = 0;
        continue;
    }
    if(rest<=0) {
        array[i] = 1;
        continue;
    }
    else {
        int randomNumber = rand.nextInt(2);
        array[i] = randomNumber;
        if (randomNumber == 1)
            payer--;
        if (randomNumber == 0)
            rest--;
    }
}

I think the error is saying that I'm trying to access an index 10 of my array which doesn't exist. I don't know where I am accessing array[10]. Or maybe the problem is somewhere else??

Thank you for your time :)




Aucun commentaire:

Enregistrer un commentaire