1

My program is designed to draw 5 cards at random from a pack of cards. However, when I run it, the output is:

null null null null null

public class Poker {
    static int numberOfPlayers;

    public static void main(String[] args) {
        drawComCards();
    }

    static void drawComCards() {
        Card[] comCard = new Card[5];
        for (int i = 0; i < comCard.length; i++)
            comCard[i] = new Card();

        for (int i = 0; i < comCard.length; i++)
            System.out.print(comCard[i].getCard() + "  ");
    }
}

public class Card {
    private String[] rank = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    private String[] suit = {"clubs", "diamonds", "hearts", "spades"};
    private String cardValue;

    void Card() {
        String cardOne = rank[(int) (Math.random() * rank.length)] + " of " + suit[(int) (Math.random() * suit.length)];
        cardValue = cardOne;
    }

    String getCard() {
        return cardValue;
    }
} 

I haven't thought about how to eliminate drawing the same card more than once yet.

5 Answers 5

6

because of this line:

void Card(){..}

remove void. This code represent method (constructors doesn't describe return type - even with void). So compiler added default no-argument constructor which initializes cardValue with null.

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

Comments

1

Change

void Card(){

to

Card(){

It is a constuctor not method

Comments

1

The problem is your "constructor"

void Card() {
    String cardOne = rank[(int) (Math.random() * rank.length)] + " of " + suit[(int) (Math.random() * suit.length)];
    cardValue = cardOne;
}

Due to the void in the declaration it is actually a method which happens to have the same name as a constructor and because there is no constructor, a default constructor is added (this is why comCard[i] = new Card(); still works). But the default constructor does not initialize cardValue so it is null when you print it.

Long story short: remove the void

Comments

0

i just tested your code, your random is fine. but you dont have constructor. this is a method not a constructor

void Card(){
    String cardOne = rank[(int)(Math.random()*rank.length)] + " of " + suit[(int)(Math.random()*suit.length)];
    cardValue = cardOne;
}

a constructor is like bellow

public Card(){
    String cardOne = rank[(int)(Math.random()*rank.length)] + " of " + suit[(int)(Math.random()*suit.length)];
    cardValue = cardOne;
}

constructor doe snot have return type. so in your code when you call

comCard[i] = new Card();

its actually calls the default constructor which does nothing. and that is why you get null. your value cardValue does not get initialized in default constructor

Comments

-2

I think you could just do rank/suit[rand.nextInt(rank/suit.length)] to get a random rank or suit

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.