-2

I'm trying to fill a deck of cards in my "Deck" class with cards created in my "Card" class. When I print the result in my Deck class, I get 52 "null"s, which tells me the two are communicating, just not very well. I've copied the code below. Where am I going wrong? Thanks!

public class Card {
    public String cardName;
    private int[] cardValue = {(Integer) null, 1, 2, 3, 4, 5, 6, 7, 8, 
        9, 10, 11};
    private String[] cardSuit = {"C", "D", "H", "S"};


    public Card(String cardName, int[] cardValue, String[] cardSuit) {
        this.cardName = cardName;
        this.cardValue = cardValue;
        this.cardSuit = cardSuit;
    }
    public String makeCard(int[] cardValue, String[] cardSuit) {
        for(int i = 0; i < 12; i++) {
            for(int j = 0; j < 4; j++) {
                cardName = cardValue[i] + cardSuit[j];
                j++;
            }
            i++;
        }
        return cardName;
    }   
}
import java.util.Arrays;

public class Deck {

    public static String[] cardDeck = new String[52];

    public String[] makeDeck(String cardName) {
        for(int i =0; i < 52; i++) {
            cardDeck[i] = cardName;
            i++;
        }

        Arrays.fill(cardDeck, cardName);
        return cardDeck;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(cardDeck));
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
P0bbn
  • 1
  • 1
  • 5
    You forgot to call `makeDeck`. And you fill the array twice. Also it should probably be `static makeDeck`. And for some reason you're making a fifty-two card deck of one card. `Card` is actually even worse. One `Card` should not have multiple suits and multiple values (except perhaps the ace, depending on the game). And you never call `makeCard`. – Elliott Frisch Feb 14 '20 at 23:10

2 Answers2

0

You create a new string array with String[] cardDeck = new String[52]; but you never assign any values to this array. String is an object and initialized with null. So only null will be printed. You never call any method of any class nor are you initializing deck and card.

Lira
  • 141
  • 2
  • 7
0

Your two classes are still independent. I've modified your code as follows:

public class Card {
    private String[] cardDeck = new String[52];
    private int[] cardValue = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    private String[] cardSuit = {"C", "D", "H", "S"};

    public String[] makeDeck() {
        int c = 0;
        for (int i = 0; i < cardValue.length; i++) {
            for (int j = 0; j < cardSuit.length; j++) {
                cardDeck[c] = cardSuit[j] + cardValue[i];
                c++;
            }
        }
        return cardDeck;
    }
}
import java.util.Arrays;

public class Deck {
    public static void main(String[] args) {
        Card card = new Card();
        System.out.println(Arrays.toString(card.makeDeck()));
    }
}

The makeDeck method in Card class will now initialize the deck by iterating over each card value in a deck and the result will be appended to the cardDeck String array.

Further, the Deck Class calls this method and displays the deck.

sudo97
  • 904
  • 2
  • 11
  • 22
  • Thanks for your help! Just so I understand everything, b/c that's the end goal, 1. `int c` is declared to hold the card value as it's inserted into the array and my code was initially flawed b/c I never created a variable to store the card value, 2. Why do I not need to list `cardValue[]` and `cardSuit[]` as parameters in the `makeDeck` method? and 3. when the `cardValue[]` element is placed in the new array, is it converted from `int` to `String` since it's now in a `String[]`, or is it still an `int`? New to learning all of this and VERY grateful for your insight! – P0bbn Feb 15 '20 at 20:10
  • 1. Yes, variable `c` is used as an index to generate the `cardDeck`. 2. Both of these variables are accessible to the entire class `Card`, so there is no need to additional specify them as parameters. 3. Yes, it is converted to String when we’re concatenating the int from `cardValue` with the string value from `cardSuit`. – Vipul Karanjkar Feb 15 '20 at 22:48