0

My name is Chris! To help improve my java programming skills, I'm trying to make a deck class which is an array of cards, however when printing the constructor for the deck class, I get the constructor name and a time stamp and I am COMPLETELY clueless as to why this is.

Can you help?

public static void main(String args[])
{
    System.out.println("Custom Java Card Game- Chris L.");
    System.out.println();     
  Deck deck = new Deck();
  deck.addCards();
  System.out.println();
  System.out.println(deck);
}

That is the code from the main program and here is the deck class:

 public Deck() //Deck is an array of cards
  {
    size = 52;
    cards = new Card[size]; //Array of cards up to 52
   //   addCards();
  }


 public void addCards(){

 for(int k = 0; k < Slength; k++){
        for (int m = 0; m < Rlength; m++){

        String s =  suite[k];
        String n =  num[m];
        int r = rank[m];

        String str = Integer.toString(r);
   //       String fin = "[" + s + "," + n + "," + str + "] \r";
    //      System.out.print(fin);
        Card card0 = new Card(s,n,r); //Makes a card! IS WORKING!
            turntoString(card0);
       //   System.out.println( turntoString(card0) );
            cards[k] = card0;   //adds card to array! IS WORKING!
        //  System.out.println(cards); //card0 
          System.out.println(turntoString(card0));
        }   
 }

and here is the output:

deck.Deck@1f01b29

Can anybody explain to me why this is?

Kultid_Games
  • 311
  • 1
  • 3
  • 10
  • 1
    And it's not a timestamp, more likely is the memory address where the object resides. – Óscar López Apr 10 '15 at 01:11
  • possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Baby Apr 10 '15 at 01:12
  • @Baby Thank you for referring me to that! I will delete if necessary. – Kultid_Games Apr 10 '15 at 01:13

3 Answers3

1

You must override the toString() method in the Deck class, otherwise the default method from Object is used, which doesn't print a very useful text. Perhaps something like this:

@Override
public String toString() {
    return "the text that you want to print";
}

You might want to return the values of the attributes in the class, or some other information that you deem useful.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thank you, sir! However, I seem to have run into a problem. I am attemping to have the toString() print out the whole cards array. However, I cannot seem to do so. Perhaps you could offer some help? public String toString(){ String con = ""; con = con + "[" + s2 + "," + n2 + "," + str2 + "] \r"; return con; } – Kultid_Games Apr 10 '15 at 01:49
  • @Kultid_Games to print an array, use the method `Arrays.toString()` – Óscar López Apr 10 '15 at 02:06
  • 1
    @Kultid_Games did this post answer your question? can it be improved? I'd like to hear your feedback ;) – Óscar López Apr 10 '15 at 17:25
  • Yes! Thank you! It was so obvious! Apologies, I would have thanked you earlier but it was very warm today! Much Thanks, -Chris – Kultid_Games Apr 13 '15 at 02:10
0

You are printing the object information itself since it doesn't have a toString() method to use in order to print it in a human readable way.

ltalhouarne
  • 4,586
  • 2
  • 22
  • 31
0
@Override
public String toString() {
    return "Any text";
}

Remember, you can override toString, equals( & hashcode) for conveniece

eg.

Comparing an instance of Deck to another deck

@Override
public boolean equals(Object object) {
      return this.size == ((Deck) object.size);

}
shepard23
  • 148
  • 2
  • 13