0

I have written 3 classes. One is a Card class, representing a playing card:

public class Card 
{
    int theRank, theSuit, theCard;

    public Card(int suit, int rank)
    { 
     theRank = rank;
     theSuit = suit;
    }

    public String getRank()
    {
     String rankString = "";

     switch (theRank)
     {
         case 1:
             rankString = "Ace";
         case 2: 
             rankString = "2";
         case 3:
             rankString = "3";
         case 4:
             rankString = "4";
         case 5:
             rankString = "5";
         case 6:
             rankString = "6";
         case 7:
             rankString = "7";
         case 8:
             rankString = "8";
         case 9:
             rankString = "9";
         case 10:
             rankString = "10";
         case 11:
             rankString = "Jack";
         case 12:
             rankString = "Queen";
         case 13:
             rankString = "King";
      }

      return rankString;
    }

    public String getSuit()
    {
     String suitString = "";

     switch(theSuit)
     {
         case 1:
             suitString = "Diamonds";
         case 2:
             suitString = "Hearts";
         case 3:
             suitString = "Clubs";
         case 4:
             suitString = "Spades";
     }

     return suitString;
  }
}

The other is a class Deck, that represents an arrayList of Card objects:

public class Deck 
{

    public ArrayList<Card> loadDeck(ArrayList<Card> deck)
    {
     for (int suit = 1; suit <= 4; suit++)
     {
      for (int rank = 1; rank <= 13; rank++)
      {
       deck.add(new Card(suit, rank));
      }
     }

    return deck;
 }

public void printDeck(ArrayList<Card> deck)
{

    for (int i = 0; i <= deck.size(); i++)
    {
     Card temp = deck.get(i);
     String rank = temp.getRank();
     String suit = temp.getSuit();
     System.out.println(rank + " of " + suit);
    }
 }

 public void shuffleDeck(ArrayList<Card> deck)
 {
    Card temp;
    int index;
    Random rand = new Random();
    for (int i = deck.size() - 1; i > 0; i--)
    {
     index = rand.nextInt(i + 1);
     temp = deck.get(index);
     deck.add(index, deck.get(i));
     temp = deck.get(i);
    }

 }

public ArrayList dealHand(ArrayList<Card> deck)
{
    Random rand = new Random();
    ArrayList<Card> hand = new ArrayList<Card>(8);

    for (int i = 0; i <= 6; i++)
    {
     int suit = rand.nextInt(3);
     int rank = rand.nextInt(51);
     Card temp = new Card(suit, rank);
     hand.add(i, temp);
     deck.remove(temp);


    }

    return hand;
}

public void printHand(ArrayList<Card> hand)
{
    for (int i = 0; i <= 6; i++)
    {
     Card temp = hand.get(0);
     System.out.println(temp.getRank() + " of " + temp.getSuit());

    }
}

}

I then have a class GoFish that contains the main method:

import java.util.ArrayList;

public class GoFish 
{

    public static void main(String[] args) 
    {
        Deck testDeck = new Deck();
        ArrayList<Card> deck = new ArrayList<Card>();
        deck = testDeck.loadDeck(deck);
        testDeck.printDeck(deck);
    }
}

The output I wanted was the rank and suit of every card in a standard 52 deck of playing cares. But there is a problem with either the filling of the arrayList or the printing of the list. Any suggestions?

Stephanie
  • 1
  • 3

2 Answers2

0

As stated by @pbabcdefp your switch statements are missing their break statements. Without the breaks, execution passes through the last case statement setting rankString = "King". Here is a link to a switch tutorial

It should be:

        switch (theRank) {
        case 1:
            rankString = "Ace";
            break;
        case 2:
            rankString = "2";
            break;
        case 3:
            rankString = "3";
            break;
        case 4:
            rankString = "4";
            break;
        case 5:
            rankString = "5";
            break;
        case 6:
            rankString = "6";
            break;
        case 7:
            rankString = "7";
            break;
        case 8:
            rankString = "8";
            break;
        case 9:
            rankString = "9";
            break;
        case 10:
            rankString = "10";
            break;
        case 11:
            rankString = "Jack";
            break;
        case 12:
            rankString = "Queen";
            break;
        case 13:
            rankString = "King";
            break;
        }

Also in the method printDeck(ArrayList) an IndexOutOfBoundsException is thrown because like normal Java arrays Lists are indexed from zero https://docs.oracle.com/javase/7/docs/api/java/util/List.html#get(int) so

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

should be

for (int i = 0; i < deck.size(); i++) 
Adrian359
  • 316
  • 5
  • 10
  • I knew arrays and arrayLists were indexed from 0, but I still don't understand why this is giving me an indexOutOfBoundsException. Aren't the two for loops you wrote above the exact same? – Stephanie Apr 27 '15 at 23:33
0

break; is needed in each end of case

roeygol
  • 4,908
  • 9
  • 51
  • 88