Need help with an array

I'm working on creating an array that is basically a deck of cards, this is the problem.

I'm getting this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

void main()

{

finalint ARRAY_SIZE = 52;

String[] deck =new String[ARRAY_SIZE];

String[] cardValue =new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit =new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

{

deck[count]= cardValue[count] + cardSuit[count];

}

I Know that this is because cardSuit is reaching the end of the array, but how do I keep this from happening and make it start from the beginning until i reach the end of the cardValue array?

[1660 byte] By [BulldogXTRMa] at [2007-9-24]
# 1
You can always check <array>.length to see if you will be in the range. If you are trying to print all the combinations of suit and values it is better to use nested loops - one by suits, the other by values.Mike
bellyrippera at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 2

What I'm trying to do is print the entire deck like this:

2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, TC, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, TD, JD, QD, KD, AD, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, TH, JH, QH, KH, AH, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, TS, JS, QS, KS, AS.

I suppose I could create a separate array for each suit but wouldn't I still run into the same problem of the arrays being different sizes?

BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 3
A little tricky math will do it.deck[count]= cardValue[count] + cardSuit[count % 4];If you have a particular order in mind you might have to shift your array, probably to {"S", "C", "H", "D"}.Drake
Drake_Duna at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 4

You will still overrun cardValue if you use cardValue[count].

What about:

void main()

{

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] cardValue = new String[]

{"2","3","4","5","6","7","8","9","T","J","Q","K","A"};

String[] cardSuit = new String[]{"C","H","D","S"};

count = 0;

for(int s = 0; s < cardSuit.length; s++)

{

for(int v = 0; v < cardValue.length; v++)

{

deck[count]= cardValue[v] + cardSuit[s];

count++;

}

}

}

}

or even:

deck[s * cardSuit.length + v] = cardValue[v] + cardSuit[s];

pbrockway2a at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 5
Sorry - extra }. And I didn't read bellyripper above.
pbrockway2a at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 6

This is what I ended up with and it worked fine

void main()

{

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] cardValue = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

deck[count]= cardValue[count % 13] + cardSuit[count % 4];

}

The next thing I need to do is to sort the cards into order and display them.

BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 7
Actually I need another method that I can call on to show an array of cards no matter what size it is.
BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 8
for (int i = 0; i < myArray.length; ++i)}x = myArray[i];...display x{
Dick_Adamsa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 9

By the way thanks for the help guys, I'm in a pinch for this assignment and I appreciate all of the help.

void main()

{

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] cardValue = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

deck[count]= cardValue[count % 13] + cardSuit[count % 4];

showCards(deck);

}

void showCards(String [] list)

{

String cards = ("card");

for (int count = 0; count < list.length; count++)

print (cards);

}

This is what I'm talking about. I need to be able to call the showCards method and have it display whatever cards are currently in the players hand regardless of how many cards there are. I believe that I'm right when I used the void declaration because I don't want it the method to return anything to itself.

Right now with the code as is, i get "card" typed out 52 times.

First how do I go about having it print out the complete deck and the order that they are in?

Is there a way to create the deck in pre-sorted like I have below?

This was the assignment:

Write a ConsoleProgram to create a deck of playing cards, shuffle them, and deal out 5 cards to the player. To do this:

?You will need an array of 52 cards. In the basic version of this program, each card is a string in the form 揦Y? where X is the rank (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A) and Y is a letter designating the suite (C, H, D, S).

?You will need to shuffle the cards by swapping the positions of two randomly selected cards in the array. For this, you should write and use three methods:

o randRange ?a method which takes 2 parameters: a minimum integer and a maximum integer. It creates a random integer in this range and returns it.

o swap ?a method which takes 3 parameters: an array of cards and two subscripts. It swaps the array elements with the given subscripts and returns nothing in its name.

o shuffle ?a method which takes 2 parameters: an array of cards and a number of times to swap cards. It shuffles by swapping two randomly selected cards the desired number of times (a possibly different pair each time). It returns nothing in its name (so it is a void method).

?You will need an array of 5 cards for the player抯 hand. After shuffling the deck, you will fill this array with the first 5 cards from the deck.

?Display the unshuffled deck, then the shuffled deck, and finally the player抯 hand. Indicate which is which in the output. For example, the output of one program run might look like this: Unshuffled: 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, TC, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, TD, JD, QD, KD, AD, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, TH, JH, QH, KH, AH, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, TS, JS, QS, KS, AS Shuffled: 2C, QC, 3S, 9C, 9S, 6C, 6H, 3C, QH, 2H, 8C, 9H, AH, 4S, AS, TH, 6D, 3D, 2S, 8S, 5C, TC, 4C, 4D, 7C, AD, 9D, JD, 4H, 5S, KH, 7H, JS, 8D, TD, KC, 3H, 2D, KD, AC, KS, JC, 5H, JH, 8H, 5D, 7S, TS, QD, 7D, 6S, QS Your Hand: 2C, QC, 3S, 9C, 9S

BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 10
Almost forgot that I'm using the Java task force acm.jarIt can be downloaded here http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar
BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 11

This is what I came up with for the showCards method and it appears to work, I'd like it to show the cards in rows, instead of typing them all out together.

void showCards(String[] list)

{

for (int count = 0; count < list.length; count++)

println(list[count]);

}

BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 12
Can anyone explain to me how to format the output string like this?Unformatted:AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KCAH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KHAD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KDAS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS
BulldogXTRMa at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 13

Hi Bulldog,

Put some newlines in:/** Prints a given list of cards in rows of len items. */

void showCards(String[] list, int len)

{

for (int count = 0; count < list.length; count++)

{

if(count > 0 && (count % len == 0)) println();

println(list[count]);

}

}

You had better check this to make sure you don't get stray unwanted

newlines at the start or at the end. Ie, convince yourself that this will

*never* happen. Also decide what you're going to do if "len" is

nonpositive.

BTW: Your use of "%" in initialising the deck only works because of

an arithmetic accident (4 and 13 are co-prime). If you alter the

program to use a pack of 40 cards (4 suits of 1 to 10). you'll

see that some cards never get included in the pack, while others

are duplicated. Since its an assignment, let me put it this way: the

marker will be looking for a double loop.

Cheers,

Peter

pbrockway2a at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 14

I guess you mean "unshuffled" rather than "unformatted".

If you print out an unshuffled pack, you fill find they are not

in this order - because you did not initialise the deck in this

order.

Your suggestion above (that you sort the deck) is

almost certainly not a good one since the question seems

to presume that you won't be using Java's inbuilt sorting

and shuffling methods. Better to use bellyripper's suggestion

(in the first reply) and initialise with a double loop.

pbrockway2a at 2007-7-16 > top of java,Java Essentials,Java Programming...
# 15

I'm not sure what you mean, when I use what you put in, the array list in the method inherits the correct parameters and values as the specified array in the main, but as soon as it loops, it breaks.

As far as the cards not getting included I did a debug and it shows the same every time, it takes the cardValue in order and pairs it with the next in line on cardSuit until the deck array is full, it does start repeating the cards, is there another way to create the deck and include each card?

void main()

{

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] cardValue = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

deck[count]= cardValue[count % 13] + cardSuit[count % 4];

showCards(deck);

}

void showCards(String[] list, int len)

{

for (int count = 0; count < list.length; count++)

{

if(count > 0 && (count % len == 0)) println();

println(list[count]);

}

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 16
In your main method you haveshowCards(deck);It should beshowCards(deck, 13);As written, it shouldn't even compile.
pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 17

I'm not sure what you're running - as the code you've posted

doesn't have a class, or a main method with the correct

signature.

When I compile and run the following:public class Dealer {

public static void main(String args[]) {

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] cardValue = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

deck[count]= cardValue[count % 13] + cardSuit[count % 4];

showCards(deck, 13);

}

static void showCards(String[] list, int len)

{

for (int count = 0; count < list.length; count++)

{

if(count > 0) {

if(count % len == 0) System.out.println();

else System.out.print(" ");

}

System.out.print(list[count]);

}

if(list.length % len == 0) System.out.println();

}

}

I get the following output:AC 2H 3D 4S 5C 6H 7D 8S 9C TH JD QS KC

AH 2D 3S 4C 5H 6D 7S 8C 9H TD JS QC KH

AD 2S 3C 4H 5D 6S 7C 8H 9D TS JC QH KD

AS 2C 3H 4D 5S 6C 7H 8D 9S TC JH QD KS

pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 18

This is my entire program so far. It seperates each set of 13 cards but it stacks them vertically.

class Dealer1 extends Program

{

void main()

{

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] cardValue = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

deck[count]= cardValue[count % 13] + cardSuit[count % 4];

showCards(deck, 13);

}

void showCards(String[] list, int len)

{

for (int count = 0; count < list.length; count++)

{

if(count > 0 && (count % len == 0)) println();

println(list[count]);

}

}

public static void main(String[] args)

{

new Dealer1().start();

}

public void run()

{ main(); }

}

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 19

I used your loop and it worked just like you said it would. Thanks!

The next thing I'd like to refine is the deck array. Now that I have the output I''m looking for.

If I add a seperate array for each suit, something like this should work I think but I can't quite figure out how.

void main()

{

final int ARRAY_SIZE = 52;

String[] deck = new String[ARRAY_SIZE];

String[] clubs = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] hearts = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] diamonds = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] spades = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

for (int count = 0; count < ARRAY_SIZE; count++)

{

deck[count]= clubs[count] + cardSuit[0];

showCards(deck, 13);

}

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 20

> This is my entire program so far.

No, it's not! There must be an "import" statement somewhere

to say what a Program is. You refer to Program in line 1, but

it's not a standard Java class.

>It seperates each set of 13 cards but it stacks them vertically.

My fault - I had "println(list[count])" instead of "print(list[count])".

Try using the improved (== corrected) showCards in my last post.

I have downloaded the .jar file you mentioned earlier, but let me

know if there are any import statements in your program, and what

command you use to compile it. (If you use a command...)

Cheers,

Peter.

pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 21

I'm sorry about that yes the import statements are

So that the entire source looks exactly like this

import acm.program.*;

import acm.graphics.*;

import acm.io.*;

class Dealer1 extends Program

{

void main()

{

final int ARRAY_SIZE = 52;

int count = 0;

String[] deck = new String[ARRAY_SIZE];

String[] clubs = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] hearts = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] diamonds = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] spades = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

while (count < 13)

{

deck[count]= clubs[count] + cardSuit[count % 4];

showCards(deck, 13);

count ++;

}

}

void showCards(String[] list, int len)

{

for (int count = 0; count < list.length; count++)

{

if(count > 0)

{

if(count % len == 0) System.out.println();

else System.out.print(" ");

}

print(list[count]);

}

}

public static void main(String[] args)

{

new Dealer1().start();

}

public void run()

{ main(); }

}

I'm using NetBeans to compile, I'm new to all of this so I'm not sure what the compile commands are that it uses.

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 22

We seem to be posting simultaneously, never mind.

I see where you're going with the various suit arrays ... and I don't

like it! You will end up dealing with a double array (an array of

arrays) and, truly, you don't want to go there.

Bellyripper's suggestion of a nested loop (reply 2) was a good one.

Read reply 4 and try it. You should end up with an unshuffled array

that looks just like the one in the assignment.

Post the whole file and any error messages if you have problems.

pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 23

Fixed the last couple of line in main to this

while (count < 13)

{

deck[count]= clubs[count] + cardSuit[0];

count ++;

}

showCards(deck,13);

}

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 24
I read it but I don't understand it, can you explain it to me?
BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 25

Post 4 worked great, thanks!

Now I need to work on the other methods

this is what I have for randRange

int randRange(int start, int end)

{

int range = end - start + 1;

return (int)(Math.random() * range) + start;

}

I'm not sure how to begin with the swap and shuffle methods though.

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 26

This is from deitel deitel's Java-How to Program-

" DeckOfCards myDeckOfCards = new DeckOfCards

for( i = 0; i < 13; i++)

{

System.out.println( " " + myDeckOfCards.dealCard(), myDeckOfCards.dealCard(),myDeckOfCards.dealCard(), myDeckOfCards.dealCard() );

}

"

(Deitel, pg 302)

SilverStreeta at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 27

> Post 4 worked great, thanks!

Good - so I assume the unshuffled deck is being displayed in the

order you expect. You might want to remove "System.out" because

your Dealer1 class extends Program so it has its own print() and

println() already defined.

randRange looks good - you should document it./** Returns an int between a given start and end value inclusive. */

int randRange(int start, int end)

{

int range = end - start + 1;

return (int)(Math.random() * range) + start;

}

> I'm not sure how to begin with the swap and shuffle methods though.

Do swap() first.

"swap - a method which takes 3 parameters: an array of cards and two

subscripts. It swaps the array elements with the given subscripts and

returns nothing in its name."

The last four words are odd. It should end by saying that swap returns

void.

What are you not sure about in the specification of swap()?

What have you written? Does it compile?

If you call your new swap method just after you have printed out the list

of cards, and then print out the list again, you should be able to see if it

is swapping a random pair of cards.

pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 28

This is what I have right now for swap(). If I understand correctly I need to make an array in this method and pull 2 elements from the array, and swap them.

void swap(String[] card1, String card2, String card3)

{

String[] card1 = new String[]{card2, card3};

do

{

//something in here

}

while (/*this happens*/);

}

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 29

This is my complete code up to this point. I've gotten the methods to compile but I'm not sure if they're going to work like they're supposed to.

Last 2 things I need help with.

1. I need help with the shuffle method. I can't figure out what it supposed to do exactly or how to begin writing it.

2. piecing this together. If I understand correctly after the deck is created, I need to

a) create the deck

b) display it with the label "Unshuffled"

c) shuffle it

d) display the shuffled deck with label "Shuffled"

e) pass the first five cards or elements of the shuffled deck array to the playerHand

f) display playerHand with the label "Your Hand"

so at this point I'm at c) shuffling the deck

import acm.program.*;

import acm.graphics.*;

import acm.io.*;

class Dealer1 extends Program

{

void main()

{

/**

* @localVar int ARRAY_SIZE. Integer representing a deck of cards without jokers.

* @localVar int count. Simple counter used to signify an element of an array while filling.

* @localVar String unshuffled. Array of strings signifying an unshuffled deck of cards.

* @localVar String cardValue. Array of strings signifying the value of a suit of cards.

* @localVar String cardSuit. Array of strings signifying a suit of cards.

* @localVar int s. Integer used as a simple counter in a loop

* @localVar int v. Integer used as a simple counter in a loop

*/

// Creates a deck of cards

final int ARRAY_SIZE = 52;

int count;

String[] unshuffled = new String[ARRAY_SIZE];

String[] cardValue = new String[]{"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

String[] cardSuit = new String[]{"C","H","D","S"};

String[] playerHand = new String[5];

count = 0;

for(int s = 0; s < cardSuit.length; s++)

{

for(int v = 0; v < cardValue.length; v++)

{

unshuffled[count]= cardValue[v] + cardSuit[s];

count++;

}

}

showCards(unshuffled,13);

}// Prints the specified deck of cards regardless of size in a neat block type matrix

void showCards(String[] list, int len)

{

for (int count = 0; count < list.length; count++)

{

if(count > 0)

{

if(count % len == 0) System.out.println();

else System.out.print(" ");

}

print(list[count]);

}

}

void swap(String[] card1, String card2, String card3)

{

String temp = card1[0]; card1[0] = card1[1]; card1[1] = temp;

}

void shuffle(String[] shuffling, int numTimes)

{

// something goes in here;

}

// Returns an int between a given start and end value inclusive.

int randRange(int start, int end)

{

int range = end - start + 1;

return (int)(Math.random() * range) + start;

}

public static void main(String[] args)

{

new Dealer1().start();

}

public void run()

{ main(); }

}

BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 30

Hi,

Your swap() looks OK (the version you last posted). Did you

test it by printing the list of cards again just after you did a swap?

Anyway, on to shuffle...

"shuffle a method which takes 2 parameters: an array of cards and a

number of times to swap cards. It shuffles by swapping two randomly

selected cards the desired number of times (a possibly different pair

each time). It returns nothing in its name (so it is a void method)."

In unplain English:

Do this ^numTimes^

-->(1) Get an index in the ^randRange^ 0 to 51

-->(2) Get another index in the same range

-->(3) ^swap^ using these two indices

"Do this etc" is obviously a for-loop with a variable going from 0

to less than ^numTimes^. The three steps go inside the loop.

Try this.

By the way, you keep changing the name of the list of cards. You

start by calling it "unshuffled",in showCards() you call it "list", in swap()

you call it "card1" and in shuffle() you call it "shuffling". But in some

sense it's the same thing.

You could try changing "unshuffled" to "deck" - because that's what it is.

The other three (in the other methods) could all be called "aDeck" to

reflect the fact that these methods work on any deck of cards.

Cheers,

Peter

pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 31
> so at this point I'm at c) shuffling the deckFor what it's worth shuffle() is the last method you have to write!c) d) e) f) take about one line each at the end of main(), soyou're not that far from the finishing post...
pbrockway2a at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 32
I can figure out how to implement the shuffling the deck. I know that I'll need to implement randRange and swap into it, but I'm just not sure how to begin, can someone take a look at what I have so far and give me a hint?
BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 33
I appreciate all of the help.I ended up with a B on this assignment.
BulldogXTRMa at 2007-7-20 > top of java,Java Essentials,Java Programming...
# 34
> I appreciate all of the help.> > I ended up with a B on this assignment.congrats, bees are good.seems to be a lot of array topics on the forum tonight.how many people made an A?kindly askingwalken
walken16a at 2007-7-20 > top of java,Java Essentials,Java Programming...