Chisx
Chisx

Reputation: 1986

The type of the expression must be an array type but it resolved to Card

I'm practically done with my project and I get this error:

"The type of the expression must be an array type but it resolved to Card"

It's on the line that reads: deck[i] = newCard[i]; Here's the code:

public class Deck
{
    //nextCard is used to keep count of which number card you are on in the deck.
    public int nextCard;
    private static final int DECK_SIZE = 52;

    //deck is used to represent an entire deck of cards.
    Card[] deck;
    //hand is a pair of 7 cards; is generated by dealACard();
    Card[] hand;

    /**
     * Default Constructor        
     *
     * <hr>
     * Date created: Feb 17, 2014 
     *
     * 
     */
    public Deck ( )
    {
        int i;
        nextCard = 0;

        for(i=0;i<DECK_SIZE;i++)
        {
            Card newCard = new Card(i);

            deck[i] = newCard[i];
        }
    }

What am I doing wrong?

Upvotes: 1

Views: 114

Answers (2)

mdewitt
mdewitt

Reputation: 2534

The code

deck[i] = newCard[i];

Means:

Put the object at index i from my Array newCard into index i in my Array deck

What you actually want is

Put this object newCard into index i in my Array deck

because newCard is not an Array, it is a Card object which is the type your Array deck holds. Just change your code to:

deck[i] = newCard;

Upvotes: 2

Reimeus
Reimeus

Reputation: 159844

Remove the array brackets to use the instance of Card that you just created

deck[i] = newCard;
                 ^

Upvotes: 2

Related Questions