Question

SO I am having a bit of trouble here in a java game of BlackJack. I have switched from using hashmaps for the card names and values and converted it over to a String[] just because it is so much easier. However, I appeared to have broken some code in it:

private void createDeck(int numCards, int numSuits) {

    deck = new ArrayList<Card>();
    cardUsed = new ArrayList<Card>();
    if ((numCards % numSuits) > 0) return;

    for (int i=0; i < numSuits; i++) {
        for(int j=1; j <= (numCards / numSuits); j++) {
            deck.add(new Card(new Suit(i), j + "", j));
        }
    }
} 

This won't run and gives me an unresolved compilation problem: The constructor Card(Suit, String, int) is undefined. However I have Card's parameters as Card(Suit, String[], int). Is there a way to go around this by still using a String array? It underlines the deck.add(new Card(new Suit(i), j + "", j)); in the program.

Here is the source for Card.java

public class Card  {

private String[] cardRank = new String[] {"Ace", "Two", "Three", "Four", 
        "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"} ;

private Suit suit = null;

private int cardValue = 0;

public Card(Suit suit, String[] cardRank, int cardValue) {
    this.cardRank = cardRank;
    this.suit = suit;
    this.cardValue = cardValue;
}

public String toString() {
    return cardRank + " of " + suit.getSuitName();
}

public String[] getCardRank() {
    return cardRank;
}

public void setCardRank(String[] cardRank) {
    this.cardRank = cardRank;
}

public Suit getSuit() {
    return suit;
}

public void setSuit(Suit suit) {
    this.suit = suit;
}

public int getCardValue() {
    return cardValue;
}

public void setCardValue(int cardValue) {
    this.cardValue = cardValue;
}

}

Was it helpful?

Solution 2

Try this:

new Card(new Suit(i), new String[]{j + ""}, j)

You were passing as second parameter j + "", which is a String. To pass a String[], which is the expected type, just pack that string into a single-element array: new String[]{j + ""}.

On a more fundamental level: why is the card rank defined as a String[] in the first place? it should be a String, a single card only has one rank! better fix the Card class and change it to have a private String cardRank as attribute:

public class Card  {

    private Suit suit = null;
    private String cardRank;
    private int cardValue = 0;

    public Card(Suit suit, String cardRank, int cardValue) {
        this.cardRank = cardRank;
        this.suit = suit;
        this.cardValue = cardValue;
    }

}

Now the original initialization code will work as intended:

new Card(new Suit(i), j + "", j)

OTHER TIPS

Well I think this is bad design generally. But for this specific Question the answer would likely be to replace j + "" with new String[] {j + ""}

You are passing in a String, not a String[]. One is just a single object, while the other is an array of objects.

j + "" results in one String, not a set of Strings.

You can solve this by placing the String into an array of Strings before passing it into your constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top