-1

What I am attempting to do is obtain a random value from my two arrays, which I have successfully done. My issue is that I am not receiving the correct output when the instance of an object from my Card class is created. I expect my output to be something like "2 of Hearts" or "Jack of Spades". Instead, I have no output but the code runs successfully.

Here is the code:

import java.util.Random;

public class Card {
    private static final String deckRanks[] = {"King", "Queen", "Jack", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    private static final String deckSuits[] = {"of Diamonds", "of Spades", "of Hearts", "of Clubs"};

    public static void main(String[] args) {

    }

    public static String getRandomCard(){ 
        String card;
        Random cardSelector = new Random();

        int randomRank = cardSelector.nextInt(deckRanks.length);
        int randomSuit = cardSelector.nextInt(deckSuits.length);

        card = deckRanks[randomRank] + " " + deckSuits[randomSuit];
        return card;
    }

    @Override
    public String toString(){
        return getRandomCard();
    }
}

And the class with the instance of the Card object:

public class CardHand {
    public static void main(String[] args) {
        Card cards = new Card();

        cards.getRandomCard();
    }
}
Idiot
  • 55
  • 5

1 Answers1

0

The main method in your CardHand class doesn't do anything to display the randomly chosen card.

It should look something like the following...

public class CardHand {
    public static void main (String[] args) {
        // create a new Card
        Card card = new Card();
        // show the output of the Card's toString() method in the console
        // remember that getRandomCard() is called when we call toString()
        System.out.println("Random card is: "+card.toString());
    }
}