0

This is my where i create my card

    package javaapplication21;

public class Card {

private String Name = null;
private String Description = null;
private String Type = null;

public Card(int i, int j) {

    if (i == 0) {
        Type = "Spades";
    } else if (i == 1) {
        Type = "Diamonds";
    } else if (i == 2) {
        Type = "Hearts";
    } else if (i == 3) {
        Type = "Clubs";
    }

    int Value = j;
    if (j == 11) {
        Name = "Jack";
    } else if (j == 12) {
        Name = "Queen";
    } else if (j == 13) {
        Name = "King";
    } else if (j == 1) {
        Name = "Ace";
    } else if (j == 14) {
        Name = "Joker";
    } else {
        Name = "" + Value;
    }

}

public String getDescription() {

    Description="Its a"+Name+"of"+Type;
    return this.Description;
}

}

    package javaapplication21;
    import java.util.ArrayList;
    public class JavaApplication21 {


        public static void main(String[] args) {
            ArrayList Deck=new ArrayList<Card>();
            for (int i = 0; i < 4; i++) {
                for (int j = 1; j < 15; j++) {
                    new Card(i, j);
                    Deck.add(new Card(i, j));


                }
            }
            System.out.println(Deck.get(5).getDescription());

        }

   }

I get an error(Cannot find Symbol) when trying to use the getDescription of the card object in index 5 of the deck. I m new to programming but i really don't know what's the problem.

2 Answers2

1

get() method of ArrayList return object of type Object class and Object class doesnt have getDescription()

You should cast it to Card class -(Card)Deck.get(5)

Rahman
  • 3,755
  • 3
  • 26
  • 43
1

The arraylist does not know about type of the stored objects. You got to define the type.

ArrayList<Card> deck = new ArrayList<Card>();
shalama
  • 1,133
  • 1
  • 11
  • 25
  • Thanks a lot. You helped me a lot all of you. Seems like I forgot to insert after the first ArrayList and that confused me for over 4 days . Thanks again for debugging my code!! – Aris Eleftheriadis Nov 14 '15 at 18:01
  • Glad that i could help you. You may mark my awnser as correct – shalama Nov 15 '15 at 12:06