0

Problem: Write a program Deal.java that takes an command-line argument N and prints N poker hands (five cards each) from a shuffled deck, separated by blank lines.

What I have:

public static void main(String[] args)
{
    int N = Integer.parseInt(args[0]);


    String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] rank = 
    { 
        "2", "3", "4", "5", "6", "7", "8", "9", "10", 
        "Jack", "Queen", "King", "Ace" 
    };

    // initialize cards in a deck
    String[] deck = new String[suit.length * rank.length];
    for (int i = 0; i < rank.length; i++)
        for (int j = 0; j < suit.length; j++)
            deck[rank.length*i + j] = rank[i] + " of " + suit[j];

    // shuffle deck
    int d = deck.length;
    for (int i = 0; i < d; i++)
    {
        int r = i + (int) (Math.random() * (d-i));
        String temp = deck[r];
        deck[r] = deck[i];
        deck[i] = temp;
    }

    // repeat for N number of people
    for (int t = 0; t < N; t++)
    {
        // print 5 random cards
        for (int i = 0; i < 5; i++)
            System.out.print(deck[i] + " ");
        System.out.println();
    }   


} 

Can anybody tell me what I'm doing wrong here? I'm getting an error ArrayIndexOutOfBoundsExceptions, not sure why. This is a problem out of an exercise set from my book, not homework.

user1164937
  • 1,979
  • 2
  • 21
  • 29
  • 5
    What you are doing wrong: Not giving any details on what isn't actually working. Not summarizing what you have done to try to correct this problem. Not giving any error messages. – RussS Feb 19 '12 at 17:56
  • +1 for @RussS - you need to do some debugging. If you cannot debug, you cannot develop software. Also, when I deal, I don't put any cards back in the pack. – Martin James Feb 19 '12 at 18:04
  • i foolishly assumed that my book had no typos whatsoever. i should've caught the part that ruakh corrected myself – user1164937 Feb 19 '12 at 18:11

3 Answers3

2

I assume you're getting an array-index-out-of-bounds exception on this line:

deck[rank.length*i + j] = rank[i] + " of " + suit[j];

where you must have meant this:

deck[suit.length*i + j] = rank[i] + " of " + suit[j];
ruakh
  • 175,680
  • 26
  • 273
  • 307
2

In addition to the index error spotted by ruakh, you are printing out the same hand N times:

// repeat for N number of people
for (int t = 0; t < N; t++)
{
    // print 5 random cards
    for (int i = 0; i < 5; i++)
        System.out.print(deck[i] + " ");
    System.out.println();
}

To print out different hands, use another index variable,

// repeat for N number of people
int j = 0
for (int t = 0; t < N; t++)
{
    // print 5 random cards
    for (int i = 0; i < 5; i++, j++)
        System.out.print(deck[j] + " ");
    System.out.println();
}   
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
0

I can't see what's the problem seems to work ok, try to give some further details.

Something you should improve is:

Check the parameter is set at the beginning properly:

public static void main(String[] args)
{
    int N = Integer.parseInt(args[0]);

String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] rank = 
{ 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", 
    "Jack", "Queen", "King", "Ace" 
};

    if(N < 0 || N > suit.length * rank.length)
      throw new IllegalArgumentException("A number between 0 and " + suit.length * rank.length + "should be provided as argument");
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59