1
import java.util.*;

public class Deck {
    public int deckSize = 52;
    public ArrayList<Card> deck1 = new ArrayList<Card>(deckSize);

    public Deck() {
        for (CardEnum card : CardEnum.values()) {
            for (SuitEnum suit: SuitEnum.values()) {
                Card newCard = new Card(card, suit);
                this.deck1.add(newCard);
            }
        }
    }

    int size() { return this.deck1.size(); }

    String draw() {
        Iterator<Card> itrCard = deck1.iterator();

        if (!itrCard.hasNext()) {
            throw new IndexOutOfBoundsException("Deck is empty!");
        }

        Card next = itrCard.next();
        String name = next.getName();
        this.deck1.remove(next);
        return name;
    }

    void shuffle() {
        Collections.shuffle(this.deck1);
    }

    public static void main(String[] args) {
        Deck deck1 = new Deck();
        deck1.shuffle();

        System.out.println("The first five cards drawn are:");
        for (int i = 0; i<5; i++) {
            System.out.print(deck1.draw() + " ");
        }
        System.out.println("\n");
        for (int i = 0; i<5; i++) {
            System.out.print(deck1.draw() + " ");
        }

        System.out.println(deck1.get(1));

        System.out.println("\nHow many cards do you want to replace? (Max of 4)");
        Scanner in = new Scanner(System.in);
        int v;
        v = in.nextInt();
    }
    //System.out.println(deck2.get(4));
}

For this segment of code it says the method get is undefined for the type Deck? How can I make the get method work? Also I am trying to be able to create a function to replace up to 4 of the cards. I can deal out old cards, but how can I keep the old ones. I also need to be able to evaluate the hand and tell what it is (full house, pair, etc). I am finding this to be quite difficult, so even if you can only answer one of my questions, I would greatly appreciate it.

3
  • 1
    Have you tried adding a method called get to type Deck? Commented Mar 15, 2015 at 23:00
  • I am confused to what I would do if I did that. Isn't the get function built into arraylist? Commented Mar 15, 2015 at 23:08
  • It is, but you're calling get on Deck, not on an ArrayList. Commented Mar 15, 2015 at 23:08

2 Answers 2

1

The Deck class itself has no get(int) method, however, you attempt to call this method on an Object of type Deck in your main() method. In order for this code to compile, you will need to write a method called get that takes an int as its argument.

Although the Deck class has an internal variable named deck1 that is an ArrayList, the scope of this method is limited to code within the Deck class. When you create a Deck named deck1 in main, this is a different object from the ArrayList named deck1 that is defined in the Deck class. Although they have the same name, these are two different objects, so this deck1 is not an ArrayList and cannot access the methods that ArrayList offers.

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

Comments

1

You just need to add a get method somewhere in your Deck class that forwards to the get method of your backing ArrayList. For example:

public class Deck {
    public int deckSize = 52;
    public ArrayList<Card> deck1 = new ArrayList<Card>(deckSize);

    public Card get(int i) {
        return deck1.get(i);
    }

Alternatively, just use the public field:

System.out.println(deck1.deck1.get(1).getName());
                     ^     ^
                     |     └-- public field
                     └--local variable

It's a bit confusing because you've given your local Deck variable the same name as your ArrayList field in the Deck class.

5 Comments

Thanks, that's what I was looking for. It returns "Card@1f01b29" though, how can I get it to return the card like it does when I use .draw()?
Glad I could help. To get the name, just add .getName() (edited above). Alternatively, you could add public String toString() {return getName();} to the Card class - then it'll print the name instead of "Card@1f01b29"
Thanks, that's exactly what I needed too. However when I get my output (The first five cards drawn are: 7 of Diamonds, Jack of Spades, Queen of Diamonds, 3 of Diamonds, 9 of Diamonds, ) I then would get the name using System.out.println(deck1.deck1.get(1).getName()); But I get Jack of Diamonds? What I want to do is select one of the cards that was already outputted.
Instead of just printing it out, put the card from deck1.draw() into a list of drawn cards, which you can then print. ...If that's not very clear, you could ask a new question just about that, after accepting the answer that best answers this current question. (Seeing that you accept answers in your previous questions encourages people to answer your future questions.)
Thanks, I think I get what you're saying. However I wasn't able to make it work with an arraylist of type Card, only with string. How can I make it work with a type of card? This is what I have now Deck deck1 = new Deck(); deck1.shuffle(); ArrayList<String> drawn = new ArrayList<String>(); System.out.println("The first five cards drawn are:"); for (int i = 0; i<5; i++) { drawn.add(deck1.draw()); } System.out.println(drawn);

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.