2

First i have a class called card with this code

public class Card
{
private int value;
private String suit;
// private int value;
//private String rank;
public Card (int v, String s)
{
    value=v;
suit=s;
}

public int random()
{
    int randomNum = ((int)(Math.random() * 100) % 13 +1);
    return randomNum;
}

public void displayCard()
{
    System.out.println(value + " of " + suit);
}

}

then i have a class called deck with this code

import java.util.*;

public class Deck 
{
public ArrayList<Card> card;
private ArrayList<String> suits;
private ArrayList<Card> hand;


public Deck()// time to build a deck
{

    card=new ArrayList<>();
    suits=new ArrayList<>();

    suits.add("Hearts");
    suits.add("Spades");
    suits.add("Clubs");
    suits.add("Diamonds");

    for (int y=2; y<15; y++)
        {
            card.add(new Card(y,suits.get(0)));
        }

    for (int y=2; y<15; y++)
        {
            card.add(new Card((y),suits.get(1)));
        }

    for (int y=2; y<15; y++)
        {
            card.add(new Card((y),suits.get(2)));
        }

    for (int y=2; y<15; y++)
        {
            card.add(new Card((y),suits.get(3)));
        }



}//end of public deck


public ArrayList deal()// deal method
{
    hand=new ArrayList<>();


    for(int x = 0; x < 5; ++x)//build 5 card hand
        {
        hand.add(card.get(x));

        System.out.println(card.get(x));

         }

    return hand;

}//end of public void deal

}// end of public class deck

then i have the main

import java.util.ArrayList;

import java.util.*;

public class gamePlay 
    {
        private static gamePlay player1;
        public Deck fullDeck;
        private ArrayList<Card> yourHand;


    public gamePlay()
    {
        fullDeck=new Deck();
        System.out.println("Your poker hand is a");
        yourHand = fullDeck.deal();

        //System.out.println(yourHand);

    }
public static void main(String[] args) 
{
player1 = new gamePlay();        
}
}

It is printing out some crazy stuff for the value and suit of the cards in the hand i think they are either memory locations from the arraylist or hexidecimal values i am not sure need it to print suit and rank any help is appreciated

3 Answers 3

4

If your classes implement a proper toString method, it will show up perfectly.

You can easily change your existing method displayCard in the Card class to a toString method. This leads to more flexibility than to let the Card print out itself by calling System.out.println in the card's method.

@Override
public String toString() {
    return value + " of " + suit;
}

If you want the card to print to System.out you just do System.out.println(card);

Normal arrays can also be converted to String, using Arrays.toString(array) (if you would have a Card[] variable for example). Most implementations of Lists already implement a proper toString method so it will show you a comma-separated list of the entries.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to provide a toString() method in your Card class.

For example:

@Override
public String toString() {
    return String.format("Card [value=%s, suit=%s]", value, suit);
}

If you don't provide that method, the default depends on the JDK implementation. Usually, it is the name of the class followed by a @ and the object hash code.

Comments

-1

I suspect that you have to iterate through the array and print each item individually. One cannot print the entire content of an array by toString() the array itself.

3 Comments

There is Arrays.toString(array) that can be used. And this question is about Lists rather than arrays.
Arrays.toString(array) is for arrays. Here we have an ArrayList, no?
yeah i have to iterate through it to print each card.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.