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.

Pang
  • 9,564
  • 146
  • 81
  • 122
asker
  • 198
  • 2
  • 11

5 Answers5

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

Change

void Card(){

to

Card(){

It is a constuctor not method

sanky jain
  • 873
  • 1
  • 6
  • 14
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

MartinS
  • 2,759
  • 1
  • 15
  • 25
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

Shawn
  • 403
  • 8
  • 38
-2

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

Ben Arnao
  • 492
  • 5
  • 11