0

I am attempting to create a program that draws 5 random cards from a deck of cards and stores the suit and value in 2 parallel arrays, then I have to check if there are duplicate cards and re-draw them if they are duplicate. To do this I have written the following code:

        for (int i = 0; i < suit.length; i++) {
            for (int j = 0; j < 5; j++) {
                randSuit = rand.nextInt(notSuit.length);
                randValue = rand.nextInt(notValue.length);

                suit[i] = notSuit[randSuit];
                value[i] = notValue[randValue];

                firstCards[i] = copyValue[randValue] + " of " + copySuit[randSuit];
            }        
    }

    for (int i = 0; i < suit.length; i++) {
        for (int j = 1; j < 5; j++) {
            if ((suit[i] == suit[j]) && (value[i] == value[j])) { // this if statement will not evaluate to true even when the conditions are true
                randSuit = rand.nextInt(notSuit.length);
                randValue = rand.nextInt(notValue.length);

                suit[i] = notSuit[randSuit];
                value[i] = notValue[randValue];

                verifiedCards[i] = copyValue[randValue] + " of " + copySuit[randSuit];
            }
        }        
    }

The bottom 2 for loops is meant to see if the card is duplicate and if so, redraw it and store that card in the verifiedCards array. Instead, it is not running and no values are put into verifiedCards[].

So my problem is, even when there are duplicates they are not re-drawn.

Example output:

Not Verified 7 of Clubs Queen of Spades 7 of Clubs 7 of Spades Ace of Spades

Verified null null null null null

As shown, 7 of Clubs is a duplicate and the card should have been re-drawn, but it was not, and I can't figure out why.
Thanks.

Edit: suit and value are int arrays of length 5.

Conhair
  • 31
  • 1
  • 6
  • how are `suit` and `value` defined? If they are `String[]`, you cannot compare them using `==`, use equals instead. – f1sh Feb 27 '20 at 17:01
  • 1
    When applied to Objects, `==` checks for *reference equality* (that you are comparing the same object in memeory), not *value equality*. – azurefrog Feb 27 '20 at 17:01
  • 3
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – sleepToken Feb 27 '20 at 17:02
  • suit and value are int arrays of length 5 – Conhair Feb 27 '20 at 17:08

2 Answers2

1

So, when you are comparing two things that are not an object, using == operator will compare the values. However, like when comparing two Strings, you use String.equals("hello") and not String == "hello"

When you're comparing an object with == you are comparing storage allocation, not values. Your program will check to see if it finds Object X at the same place it finds Object Y.

So, to fix your program, you simply need to change how you're comparing the values you are referencing.

Josh Heaps
  • 335
  • 1
  • 8
  • is this still true even when suit and value are integers? Sorry I did not clarify that in the initial question. – Conhair Feb 27 '20 at 17:11
  • If they were JUST integers, yes. In this case, however, you have an Array of integers, and when you get a value from the array using 'array[0]' it will treat it like an object for comparison purposes. When you want to change the value, you can reference it like this: 'array[0] = array[1] + 5;' or something like that. But when you make a comparison using the '==' operator, it will compare the storage space allocation of the reference, and not the value itself. – Josh Heaps Feb 27 '20 at 17:15
  • So, more simply put, yes, when suit and value are integers. But suit and value are an array of integers, which makes them an object. – Josh Heaps Feb 27 '20 at 17:16
  • Does that help? (Honestly, you should try it out. It'll work.) – Josh Heaps Feb 27 '20 at 17:25
  • So whenever I change the statement to suit[i].equals(suit[j])... it gives me a compliing error: int cannot be dereferenced. Do you mean i should be sing the array .equals method or just the same one that you use for strings? – Conhair Feb 27 '20 at 17:53
  • Oh my heavens it's been too long since I've used Java. I'm sorry I totally forgot that there was a different .equals for an array. Yes you should use the array one. – Josh Heaps Feb 27 '20 at 18:04
  • Ok, the only issue with that is that I don't think I can test specific indexes using Arrays.equals, only whole arrays, though I may be wrong on that. – Conhair Feb 27 '20 at 18:05
  • ... Hold on you're right. I've been using C# at work so I have not touched java for like, 3 years. Give me a second and I'll give you a not stupid answer haha – Josh Heaps Feb 27 '20 at 18:08
  • All good! Thanks for your help! – Conhair Feb 27 '20 at 18:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208665/discussion-between-josh-heaps-and-conhair). – Josh Heaps Feb 27 '20 at 18:15
0

The problem was that the if condition did not contain "&& i != j" at the end as that was causing problems. Thanks for everyone's input!

Conhair
  • 31
  • 1
  • 6