help altering code in one of my methods.

I created the class card

class Card
{
  private int rank;
  private char suit;
 
  public int getRank()
  {
    return(rank);
  }
 
  public char getSuit()
  {
    return(suit);
  }
 
  public Card(int r,char s)
  {
    rank = r;
    suit = s;
  }
 
 
  public boolean equals(Card otherCard)
  {
    if((rank == otherCard.rank) &&
       (suit == otherCard.suit))
      return(true);
    else
      return(false);
  }
 
  public void displayCard()
  {
    System.out.println(rank + " of " + suit);
  }
 
}

i then created the class deskOfCards

import java.util.*;
import java.util.Collections.*;

class DeckOfCards
{
  private ArrayList<Card> card = new ArrayList<Card>();
 
 public DeckOfCards()
  {
   
    for(int i=0; i < 52; i++)
    {
      if(i < 13)
        card.add(new Card(i%13+1,'S'));
      else        
        if(i < 26)
            card.add(new Card(i%13+1,'H'));
       
      else
        if(i < 39)
              card.add(new Card(i%13+1,'D'));
      else
              card.add(new Card(i%13+1,'C'));
   
   
     
    }  
     
   
  }
 
  public void shuffle()
  {
    Collections.shuffle(card);
  }

  public Card pickNextCard()
  {
    return card.remove(0);
  }
 
}

and also the class

import java.util.*;

public class HandOfCards
{
  public ArrayList<Card> card1;

public HandOfCards()
{
  card1 = new ArrayList<Card>();
}

public void addCard(Card s)
{
  card1.add(s);
}

public void display()
{
  for(int i = 0; i <= card1.size(); i++)
  {
  card1.get(i).displayCard();
 }
}

public int totalScore()
{
  int cardvalue = 0;
  int sum = 0;
  for(int i = 0; i <= card1.size(); i++)
  {
    cardvalue = card1.get(i).getRank();
    sum = sum + cardvalue;
  }
  return(sum);
}

public boolean isBust()
{
 if (totalScore() > 21)
  return(true);
 else
   return(false);
}
}


I then made another class with a main method to run and test these classes

import java.util.*;

public class Pontoon
{
  public static void main(String[] args)
  {
    int stickortwist = 2;
    Scanner keyboard = new Scanner(System.in);
    DeckOfCards newDeck = new DeckOfCards();
    newDeck.shuffle();
    HandOfCards newHand = new HandOfCards();
    newHand.addCard(newDeck.pickNextCard());
    newHand.addCard(newDeck.pickNextCard());
    System.out.println("Press 1 to stick, or 2 to twist");
    stickortwist = keyboard.nextInt();
    if (stickortwist == 2)
   {
    newHand.addCard(newDeck.pickNextCard());
    System.out.println("Press 1 to stick, or 2 to twist");
   stickortwist = keyboard.nextInt();
    }
    newHand.totalScore();
  }
}

When i try and run the main method i get the error

IndexOutOfBoundsException: Index: 3, Size: 3
  at java.util.ArrayList.RangeCheck(Unknown Source)
  at java.util.ArrayList.get(Unknown Source)
  at HandOfCards.totalScore(HandOfCards.java:31)
  at Pontoon.main(Pontoon.java:22)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)


so i think i need to change the code for the totalScore method but i don't know what is wrong with it.

thanks for your help

andybestAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>for(int i = 0; i <= card1.size(); i++)

should be

for(int i = 0; i < card1.size(); i++)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
Yes, array index starts from 0 in Java (like it did in C, C++ too) so the last element is at (size - 1) :)
0
andybestAuthor Commented:
wow, that was simple. cheers for the help.
0
CEHJCommented:
:-)
0
CEHJCommented:
btw you might like to look at the Java blurb on Enum. Card is a perfect candidate for enum and in fact is used for one of Sun's examples
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.