user3538292
user3538292

Reputation: 3

Incompatible type card cannot be converted to string

Not sure how to fix this error, I commented where the error in the code, very new to java. I am trying to send my cards into my constructor and create a deck array.

public abstract class CardGame {
    protected String[] deck = new String[52];
    private String[] Suit = {"Spade","Dimond","Heart","Club"};
    private String[] Card = {"2","3","4","5","6","7","8","9","10","jack","queen","king","ace"};

public CardGame(){
    int c = 0;
   for (int x=0; x < Suit.length; ++x){
       for (int i = 0; i < Card.length; ++i){
           deck[c] = new Card(Suit[x], Card[i]); // heres the error
           c++;
       }

    } 

}

}

public class Card {
   String suit;
   String name;

public Card(String s,String n){
    suit = s;
    name = n;
}

public String getsuit(){
   return suit;
}

public String getname(){
   return name;
}

}

Upvotes: 0

Views: 1000

Answers (1)

MarsAtomic
MarsAtomic

Reputation: 10696

Here's the problem:

protected String[] deck = new String[52];

You have an array representing your cards as type String, when you probably want to have an array of type Card if you intend to insert cards into that deck.

For your problem line to work:

deck[c] = new Card(Suit[x], Card[i]);

You'll need to declare deck like so:

protected Card[] deck = new Card[52];

Upvotes: 4

Related Questions