Pack of cards creation

Hi,

I am trying to create a pack of cards labeled like this: ah, 2h etc...

This is the output i get from the code at the bottom of this post:

10h
jh
qh
kh
ad
2d
3d
4d
5d
6d
7d
8d
9d
10d
jd
qd
kd
as
2s
3s
4s
5s
6s
7s
8s
9s
10s
js
qs
ks
ac
2c
3c
4c
5c
6c
7c
8c
9c
10c
jc
qc
kc

I cant understand why the ah, 2h, 3h, 4h, 5h, 6h, 7h, 8h, 9h don't get created.

public String[] pack = new String[52];//Declared as class variable

public void Pack()
{

String card = "";
String suit = "h";
int packCounter = 0;
int n = 1;

while (packCounter <4)
{

for (int i = 0; i<=52; i++)
{

if ((n > 1) && (n < 11))
{
card = n+suit;
pack[i] = card;
n++;
System.out.println(card);
}

else if (n == 1)
{
card = "a"+suit;
pack[i] = card;//produces an ArrayIndexOutOfBoundException
System.out.println(card);
n++;
}

else if (n == 11)
{
card = "j"+suit;
pack[i] = card;
System.out.println(card);
n++;
}

else if (n == 12)
{
card = "q"+suit;
pack[i] = card;
System.out.println(card);
n++;
}

else if (n == 13)
{
card = "k"+suit;
pack[i] = card;
System.out.println(card);
n=1;
packCounter++;

if (packCounter == 1)
{
suit = "d";
}

else if (packCounter == 2)
{
suit = "s";
}

else if (packCounter == 3)
{
suit = "c";
}
}
}

}
}


Any help would be most grateful

Thanx
[2397 byte] By [cpb123] at [2007-11-18 17:46:06]
# 1 Re: Pack of cards creation
Wouldn't it be a lot simpler to do something like this?public class Pack {
private String[] pack = new String[52];
char[] suits = { 'h', 'd', 'c', 's' };
String[] values = { "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k" };

public Pack() {
int card=0;
for (int suit = 0; suit < suits.length; suit++) {
for (int value = 0; value < values.length; value++) {
pack[card] = suits[suit] + values[value];
System.out.println(pack[card]);
card++;
}
}
}
}Imagination is more important than knowledge...
A. Einstein
dlorde at 2007-11-10 2:30:33 >
# 2 Re: Pack of cards creation
you might find the following code is more sensible than throwing IFs in all over the place:

String[] suits = {h, d, c, s}
String[] values = {a,2,3,4,5,6,7,8,9,10,j,q,k}
String[] pack = new String[52];

int packCounter =0
int suitCounter =0
int valueCounter =0

for(suitCounter = 0; suitCounter<suits.length; suitCounter++){
for(valueCounter = 0; valueCOunter<values.length; valueCoutner++){
pack[packcounter] = suits[suitcounter] + values[valuecounter]
packcounter++
System.out.println(pack[packcounter])
}
}

THE POSTED CODE DOES NOT COMPILE
It containes numerous syntactical/lexical and spelling errors. There is also a order-of-statements problem that will cause it to print nulls instead of the card values and finally crash with an arrayIndexOutOfBoundsException

These errors are deliberate, because i believe your assignment to be an academic one, and hence im not about to do your homework for you. Correcting the errors will also be a good learning experience :)
cjard at 2007-11-10 2:31:24 >
# 3 Re: Pack of cards creation
or, you could just print dlorde's code and hand it in...

heh. it's nice to know though, that great minds post alike/fools seldom differ (both mine and his code, contain TWO of the exact same bugs that cause our proposed solutions to violate the spec - mine were deliberate.. Im wondering; were dave's, and how does it come to be that we made identical errors, posting at the same time? ) ;)
cjard at 2007-11-10 2:32:26 >
# 4 Re: Pack of cards creation
If it matters, my 'errors' were not deliberate - I was just approximating what was required without paying much attention to the fine detail. I would expect someone to adapt the code to their purposes, which is why I said "Wouldn't it be a lot simpler to do something like this?"

A good programmer always looks a gift horse in the mouth...
dlorde at 2007-11-10 2:33:32 >
# 5 Re: Pack of cards creation
Thank you for the help but i dont know what a java.lang.StackOverflowError is and i dont know how to correct it
cpb123 at 2007-11-10 2:34:33 >
# 6 Re: Pack of cards creation
Originally posted by cpb123
Thank you for the help but i dont know what a java.lang.StackOverflowError is and i dont know how to correct it What StackOverflowError ?

The prize for non-sequitur of the week goes to cpb123...
dlorde at 2007-11-10 2:35:32 >
# 7 Re: Pack of cards creation
it must have been a program error as when i deleted the class files and re-compiled my code no errors occured.

Thanx
cpb123 at 2007-11-10 2:36:38 >