Hi, I need to write a pontoon game. So far i wrote the Deck and Card classes. When i try to print the toString in my deck class i get "Deck@6612fc02". What i wanted is to print 13 cards on each row. May you help me thanks.

public class Deck
{
    private LinkedList<Card> card = new LinkedList <Card>();
    private Iterator <Card> it;
    private char[] suit = {'H','D','C','S'};

    
    public Deck()
    {
        for(int i = 0; i< 4; i++) 
        {
            for(int j = 1; j <= 13; j++)
                card.add(new Card(j,suit[i]));                             
        }  
    }
    
    public void show()
    {
        System.out.println(toString());
    }

    
    public String toSring()
    {
        String s = " ";
        
        for(int i =0; i < 13; i++)    
        {
            s = card.toString() + "\n";
        }        
        return s;
    }
    
}
public class Card
{
    private int value;
    private char suit;
    
    public Card(int value, char suit)
    {
        this.value = value;
        this.suit = suit;
    }
    
    public String toString()
    {
        String s= suit + " " + value;
        return s;
    }
}

First, i suppose you're making a text-based game, running in a console window ?

Second, in order to print your cards, you need to define what to print.

Your computer doesn't know it needs to draw card shapes just because you named your class 'Card'.

The toString() method does what it says, it prints the object, i.e. the pointer to the object in the memory.

If you want actual cards to show, search for ASCII images, and try to print that for every card in your deck, in your show() method.

print(anyObject) automatically calls anyObject.toString() to get a printable version. All objects inherit a version from the class Object, which shows the class name and a hex reference for the particular instance, eg "Deck@6612fc02".
You can override toString in your own classes to print something more useful.
You did this for Card, but not for Deck. And before you disagree with me, look again more closely at Deck line 23.

Thanks, I made a silly mistake

You won't be the last one of us to do that either!
It's a good idea to mark override methods with an @Override annotation - it lets people know you are overriding and if you get it wrong the compiler will warn you that its not a correct override.
I suppose you have already noticed that there's no point going round a loop 13 times if you do the same thing every time?

Yes, Thankyou.

I realise it prints all the cards in a line.

Yes, card is a LinkedList, and LinkedList overrides toString to return a comma-delimited string enclosed in {} and containing the results of calling toString on each of the elements in the list in turn. Very useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.