0

I am making a poker game in java using eclipse, I need help with creating a code to find a PAIR for a single line of input. If the user enters: 12,11,5,2,12 then how can I identify if the input has two of the same numbers then print it as a PAIR.

So basically I've already taken a string input and then converted it into a String array with a split function to identify the numbers and then made it into a Int array.

When a user input: 12,3,4,5,12 -- it should print in the output: PAIR

5 Answers5

2

Just use a set, and add the element. If add returns false, there is a dupe, and you can do whatever logic you want. https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#add(E)

Set<String> cards = new HashSet<>();
if (!cards.add(something))  {
    // match of two cards found
}

If you need it to be numbers, use Integer, not int, since the latter is a primitive not an obj.

angryip
  • 2,140
  • 5
  • 33
  • 67
0

Try using a set. An element can only once be contained in a set, so contains() will return true if you have duplicate numbers in your array.

public static void findPairs(int[] numbers) {
  Set<Integer> set = new HashSet<>();
  for (int number: numbers) {
    if (!set.add(number)) {
      System.out.println("PAIR: " + number);
    }
  }
}

Keep in mind that if your array contains more than 2 duplicates of the same number, this will print PAIR: n more than once - it will not find more than one duplicate of a given number in a list.

In that case you might want to consider removing the number and keeping track of which pairs you already caught.

Tom
  • 16,842
  • 17
  • 45
  • 54
amq
  • 160
  • 4
  • 17
  • 1
    You could simplify the code by using the fact that `add()` method returns `true` when the element didn't existed in set and `false` otherwise: `if (!set.add(number)) { System.out.println("PAIR: " + number); }` – MartinBG Oct 12 '19 at 14:01
  • thanks for pointing that out. should compile now and also work with the array provided – amq Oct 12 '19 at 14:07
0

You can use a array where position+1 is the number of the card, for example:

5 5 10 11 7

[0, 0, 0, 0, 2, ... , 1, 0, 0, 1, 1,...]

0

How is the user input stored. For example store it as an array and the following would output as asked.

public static void main(String[] args) {
    int[] nums = new int[]{12,11,5,2,12};
    System.out.println(isPair(nums));
}

private static String isPair(int[] nums){
    Set<Integer> set = new HashSet<>();
    for(int n : nums){
        if(set.contains(n)){
            return "PAIR";
        }else{
            set.add(n);
        }
    }
    return null;
}
kar
  • 4,791
  • 12
  • 49
  • 74
0

As the other answers point out, using a Set makes it easy to find duplicates, but can't tell the difference between a pair, trips, and quads, or between two pair and full house. It also doesn't help find the rest of the poker hands.

A better approach is to simply sort the array of numbers you're using to represent cards, then iterate through the sorted list looking for pairs and triples (which will now be adjacent). This will also help you look for straights, and help with ranking between similar hands.

Also, it's generally best to look for hands from the top down: i.e., check for straight flush, then quads, then full house, then flush...down to one pair and no pair.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • How do I sort these arrays and also how should I approach this because im new to programming in java and I need something down for my CS class. – randomuser15234 Oct 15 '19 at 16:34
  • Java's built in sort function is fine. Checking for pairs simply iterating through the list (you've gotten to for loops, right?) looking for cases where a[i] == a[i+1] (or however you arrange it). – Lee Daniel Crocker Oct 15 '19 at 16:42