www.webdeveloper.com
Results 1 to 3 of 3

Thread: sending an array to a method

  1. #1
    Join Date
    Nov 2003
    Posts
    128

    sending an array to a method

    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?

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    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.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  3. #3
    Join Date
    Nov 2003
    Posts
    128

    Talking

    thanks (=

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles