# Thread: Need help with pokerhand/two pairs

1. Member
Join Date
Nov 2012
Posts
12
Rep Power
0

## Need help with pokerhand/two pairs

I need to create a program that checks whether the randomly assigned hand contains two pairs. Now this could be done with one heck of an if - method containing all the possible variations of two pairs. Another way I figured would be to sort the array and check whether any two indexes that are alongside were equal. However, I cannot use Arrays.sort.

Now I'm at a loss on how to proceed. The for - methods I thought of that would put each member of the array against each other would also count in three of the same and four of the same.

Any hints on how to proceed here?

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: Need help with pokerhand/two pairs

Why can't you use sort()? If it's an arbitrary assignment condition, then perhaps you are meant to implement your own sort method for the array.

Sorting would make sense, not only for detecting two pairs (remember to distinguish 2 pairs from 4 of a kind) but for doing other hand evaluations.

---

Another approach would be to create an array of 13 ints. Use a for loop to go through the hand counting the card values. Then another for loop could go through the array of face value counts and see how many 2's there are.
Last edited by pbrockway2; 12-01-2012 at 12:41 PM.

3. Member
Join Date
Nov 2012
Posts
12
Rep Power
0

## Re: Need help with pokerhand/two pairs

I decided to go with the one heck of an if-statement. It's clumsy as hell but works.

Also need to work out this: I need to print out the number of the card and whether it's a club, heart etc.

I can parse out the information of the type and turn it to integers 1-4. Is there anyway to attach a string to each of these values? So if the card's type has a value of 1, it would print out "spades"?

edit: I cannot use sort() because this assignment is done in artificial environment. I can only type certain amount of code; the rest is given. And the it hasn't imported java.util.Arrays.

4. Member
Join Date
Nov 2012
Posts
12
Rep Power
0

## Re: Need help with pokerhand/two pairs

Ooooh yeah, baby! Got it working!

Well, with the clumsy huge if-method but still.

The second part was so easy it's embarassing. Just created a String-array.

5. ## Re: Need help with pokerhand/two pairs

Originally Posted by VIhuri
I can parse out the information of the type and turn it to integers 1-4. Is there anyway to attach a string to each of these values? So if the card's type has a value of 1, it would print out "spades"?
Originally Posted by VIhuri
The second part was so easy it's embarassing. Just created a String-array.
Have you learned Enums yet?
Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

db

6. Member
Join Date
Nov 2012
Posts
12
Rep Power
0

## Re: Need help with pokerhand/two pairs

Nope. I've encountered the word in error messages but that's about it.

I would like to hear how the two pairs problem would have been solved in a more efficient way. The tools we've been given in this class thus far aren't exactly very refined (if, while/for, tables, subprograms)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•