-1

My Arraylist is an Arraylist made out of enums which have Suits and Ranks (all together 52cards)

    ArrayList<Card> deck = new ArrayList<Card>();
    Random rand = new Random();

I created my card deck and shuffled it and now I am trying to pick a random Card.

    public Card dealCard() {

    int index = rand.nextInt(deck.size());
    Card cards = deck.get(index);
    return cards;

But my main is giving me Errors

    public static void main(String[] args) {
            Deck deck1 = new Deck();
            deck1.dealCard();
            System.out.println(deck1.toString());

Any ideas on how I could pick that random card? I suppose it has something to do with that int and rand.nextInt as I am not picking Integers. But how can I pick my cards? Should I use my Suite.values() and Rank.values() here as well? And how do I apply them for random? *edit: my class Card contains the enums and setters and toString and my class Deck has the methods for creating the Deck shuffling it and picking one card and then I have the static void main for running it

Bruthzilla
  • 23
  • 1
  • 5
  • 2
    What is the error? – denvercoder9 Dec 10 '16 at 20:09
  • 1
    The code you've posted looks OK. Post the error and a [minimal and complete](http://stackoverflow.com/help/mcve) example of the code that raises the error. – teppic Dec 10 '16 at 20:17
  • trying.Deck@6d06d69c while trying is my package – Bruthzilla Dec 10 '16 at 20:18
  • 2
    That's not an error. That's the output of your print statement. Read this thoroughly: http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – denvercoder9 Dec 10 '16 at 20:21
  • added the whole code now – Bruthzilla Dec 10 '16 at 20:24
  • There is no `toString()` method in `Deck` class and yet you are calling it. The result is that `Deck` class inherits a `toString()` method by default from the `Object` class and that is being called. And that method simply prints the hashcode of the object. You already have a `print` function in `Deck` class. Why do you need to call `toString()` on `Deck`. Just call `deck.print()` after `deck.dealCard()` – denvercoder9 Dec 10 '16 at 20:26
  • Your `dealCard()` is returning a random card from the deck, but is not removing it from the deck, so you may get the same card again next time you deal a card. If this is OK, it’s OK. – Ole V.V. Dec 10 '16 at 20:32

1 Answers1

1

The first issue I see is that you are returning the card from your dealCard() function, but not saving it anywhere. Try changing the last two lines to System.out.println(deck1.dealCard().toString());

Also I believe you are missing a } to close your dealCard() function.

Anthony Palumbo
  • 314
  • 1
  • 11