Click to See Complete Forum and Search --> : sending an array to a method


davey
02-03-2005, 07:43 PM
public static int total(int[] cards){
int total=0;
for(int i=cards.length; i>0; i--){
total+=cards[i];
}
return total;
}
public static void main(String[] args) {
int[] dealerhand = new int[21];
dealerhand[1]=dealcard();
dealerhand[2]=dealcard();
dealerhand[0]=total(dealerhand);
}

i want to do this but it says
java.lang.ArrayIndexOutOfBoundsException: 21
this error occurs in the method and i believe i need to set cards to be 21 spaces but i dont know how
can you help me?

ray326
02-03-2005, 08:10 PM
for(int i=cards.length; i>0; i--){

for (int i=0; i < cards.length; i++) {
...

cards[cards.length] is beyond the end of the cards array.

davey
02-03-2005, 08:36 PM
thanks (=