0

I am needing help creating an ArrayList and adding some Integers to them. Essentially, I have a card game that pulls the card and then saves the ID (BCID). I want to add this pulled ID to an ArrayList, so I can avoid pulling the same card twice. I have it setup as below, but I keep getting duplicates. I know the cards are pulling correctly, as it displays everything fine - just repeated cards, unfortunately.

Any help you can provide would be great! I have done my best just to include the relevant parts. If you need additional information, let me know.

public static Integer bcid1, bcid2, bcid3, bcid4, bcid5, bcdcid;
public static List<Integer> usedCards = new ArrayList<Integer>();

In the example below, it is supposed to detect the duplicate then initialize the sequence to draw a different card.

    public static void setIDs() 
{
    try 
    {
        bcid1 = Integer.parseInt(bc1);
        usedCards.add(bcid1);
    }
    catch(NumberFormatException nfe)
    {       }

    try 
    {
        bcid2 = Integer.parseInt(bc2);
        if (usedCards.contains(bcid2))
        {
            try 
            {
                blueCard2(ctx);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            usedCards.add(bcid2);
        }
    }
    catch(NumberFormatException nfe)
    {       }

    try 
    {
        bcid3 = Integer.parseInt(bc3);
        if (usedCards.contains(bcid3))
        {
            try 
            {
                blueCard3(ctx);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            usedCards.add(bcid3);
        }
    }
    catch(NumberFormatException nfe)
    {       }

    try 
    {
        bcid4 = Integer.parseInt(bc4);
        if (usedCards.contains(bcid4))
        {
            try 
            {
                blueCard4(ctx);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            usedCards.add(bcid4);
        }
    }
    catch(NumberFormatException nfe)
    {       }

    try 
    {
        bcid5 = Integer.parseInt(bc5);
        if (usedCards.contains(bcid5))
        {
            try 
            {
                blueCard5(ctx);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            usedCards.add(bcid5);
        }
    }
    catch(NumberFormatException nfe)
    {       }
}
1
  • 1
    I do not follow your logic. Where bc1, bc2, etc. come from? Commented May 21, 2013 at 1:46

2 Answers 2

1

Use HashSet instead of arraylist. Set does not allow duplicate.

Sign up to request clarification or add additional context in comments.

3 Comments

Won't this be counter-productive? If it won't accept a duplicate, how can I get to draw a new card in the if statement?
This would be logical and good answer, but I think he/she is expecting a solution not a direction for solution :)
And Marko is somewhat right. Since I have never used HashSet before, any specific direction you can provide would be great!
0

First consider using methods for repetitive code it is easier to debug and adjust all at one place not jumping around. Here if you want to use lists follow something like this. Adjust accordingly

EDIT: Look this is not going to work for many reasons, because list.contains returns true only when an object is inside of a list, in your idea of solution you have two different objects with the same content. You are mixing two of this things. It is not the same. So your function only returns false.

My code was more of a suggestion for you how to create a method, and was hoping that you are going to put some more work in this. These are basic programming skills. I don't want to be rude, but this is totally out of purpose of this forum.

Hope this helps and enjoy your work

2 Comments

Unfortunately, it is still pulling duplicate cards. Any ideas?
Hello Marko, I appreciate your response. I've been researching for the past 5 hours, and I've just been having trouble wrapping my brain around why it wouldn't work. Your response gives some insight though. I will continue digging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.