0

i have a problem with my script

i have a method that return a card, another return the name, another the value and another the suit

public Card pickRandomCard() {
        while (rand < 6) {
        System.out.println(deckOfCards.get(rand));
        int cardValuePlayer1 = (deckOfCards.get(rand).toInt());
        currentTotal1 = cardValuePlayer1;
        String name1 = (deckOfCards.get(rand).toString());
        nameF = name1;
        String suit_1 = (deckOfCards.get(rand).suit());
        suit1 = suit_1; 
        return deckOfCards.get(rand++);
    }
        return null;

    }

    public int totalValuePlayer1() {
        return currentTotal1;
    }

    public String name1() {
        return nameF;
    }

    public String suit_1() {
        return suit1;
    }

but now, i want to create an arraylist of cards, like a hand of cards, so my ideia is create an arraylist with 5 cards, but if i did something like

    List<Card> hand = new ArrayList<Card>(); 
    Iterator<Card> iter = hand.iterator();

    while (iter.hasNext()) {
        hand.add(gr.pickRandomCard());
    }

i receive Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

i have this code in another class to call each method, suit, value of card, image, but i need to create a list to compare cards, and with the code below, it is impossible to compare a card one by one

    ActionListener one = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (gr1.getCounter1() < 5) {
                gr1.setCounter1(gr1.getCounter1() + 1);
                test1.setIcon(play1a);
                pn1.setText(Integer.toString(play2a));
                pn5.setText(play3a);
                pn50.setText(play4a);
            } else {
                pn5.setText("No more cards");
            }
        }
    };

    arraybtn[1].addActionListener(one);
    arraybtn[1].setPreferredSize(new Dimension(120, 20));

    play1a = gr.pickRandomCard().getImage();
    play2a = gr.totalValuePlayer1();
    play3a = gr.name1();
    play4a = gr.suit_1();

    arraybtn[1].setText(play3a);//change the name of button

i think the problem is that i need to create 4 list, each for correspondent method (suit, image, value), but i don't have any idea how i can do that

thanks

4 Answers 4

2

In your code there is a flaw:

// this iterates over existing cards
Iterator<Card> iter = hand.iterator();

// but there are no cards yet
while (iter.hasNext() /* this will never return true */ ) {
    // so this is never executed
    hand.add(gr.pickRandomCard());
}

Try this instead:

List<Card> hand = new ArrayList<Card>(); 
for(int i = 0; i < 5; i++){
    hand.add(gr.pickRandomCard());
}
Sign up to request clarification or add additional context in comments.

Comments

2
 List<Card> hand = new ArrayList<Card>(); 
 Iterator<Card> iter = hand.iterator();

 while (iter.hasNext()) {
     hand.add(gr.pickRandomCard());
 }

Your "hand" is empty, therefore you have no elements. That's why your size is 0;

Also you can create a new class, say Playa, that has name, suit, image, value attributes.

2 Comments

You probably mean Player. Playa would have attributes like sand, ocean, palm trees,etc :-)
@Sean don't be a Playa-Hater ;)
1
 while (iter.hasNext()) {
        hand.add(gr.pickRandomCard());
    }

iter is from the empty list you just created so hasNext() will always return false;

You are mentioning concepts like suit and hand but already deciding they should be ArrayLists. Why don't you create Suit and Hand classes. Hand will probably contains some collection of cards and a construtor or method that populates it. Suit should be an enum like

enum Suit{
  CLUBS,
  SPADES,
  HEARTS,
  DIAMONDS
}

If you model your data correctly in an OO fashion you'll find your code easier to write.

Comments

0

To build on Sean's answer, the reason why you can't iterate your hand ArrayList is because it doesn't have any items. So you need to use a for loop to add them.

Comments

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.