Results 1 to 3 of 3
  1. #1
    angerk is offline Member
    Join Date
    Feb 2014
    Rep Power

    Default Creating instances to represent a deck of cards

    As per the title, I am creating a deck of 52 cards each with their rank(1 2 3...King, Ace) and suit(Clubs... Spades).

    I want to be able to print out value of each card after creating them such as - 2 of CLUBS and so on. I am able to do that now. Only problem is, when it comes to the picture cards, I am representing them with numbers 11(Jack), 12(Queen) 13(King) and 14(Ace). I can't quite get around to changing the numbers to be represented as the actual words (King and so on).

    I imagine that I should store the jack,queen,king,ace values in an enum too and call it in my loop but can't quite get it. Should I have like a 3rd for loop in my following codes to make it work? Please advice. Thank you.

    Java Code:
    //My main method. The Card class follows below
    public static void main(String[] args) {
            Card[] cards = new Card[52];  
            int i = 0;
            for (Card.Suits suit : Card.Suits.values()) { 
                for (int y = 2; y < 15; y++) {
                    cards[i] = new Card(y,;  
            for(Card p : cards){ 
                System.out.print(p.format() + " "); 
    //Card Class
    public class Card {
        public enum Suits{
        public enum Faces{
            JACK, QUEEN, KING, ACE;  
        private int rank;
        private String suit; 
        public PlayingCard(int rank, String suit){ 
            this.rank = rank;
            this.suit = suit;
        public int getRank(){
            return rank; 
        public String getSuit(){
            return suit;
        public String format(){
            return String.format("%d of %s, ", getRank(), getSuit()); 

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power

    Default Re: Creating instances to represent a deck of cards

    Your Suits enum is fine, but I would create a Rank enum that contains constants for all the possible rank values, not just the face cards. You can't start an identifier with a number, but you could name them ACE, TWO, etc.

    Enums are actual types; you can declare a field of their type. Get rid of your int and String fields for rank and suit and replace them with your enum types. It would be good practice to make them final, too.

    Java Code:
    class Card {
       private final Rank rank;
       private final Suit suit;
       // etc.
    It's good practice to use Collections instead of arrays unless you have a compelling reason to use an array. It would simplify your loops, too:

    Java Code:
    List<Card> cardList = new ArrayList<Card>(52);
    for(Card.Suits suit : Card.Suits.values()) {
       for(Card.Rank rank : Card.Rank.values()) {
          cardList.add(new Card(rank, suit));
    It looks like you tried to write a constructor there, but it doesn't have the same name as the class.

    If you override toString(), you can output cards like this:

    Java Code:
    for(Card card : cardList) {
    Last edited by kjkrum; 02-12-2014 at 09:18 AM.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Sutured is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default Re: Creating instances to represent a deck of cards

    You can do this as well

    Java Code:
    public enum Value {
    	private int rank;
    	private Value(int rank) {
    		this.rank = rank;
    	public int getRank() {
    		return this.rank;
    You compare integers this way. Like

    Java Code:
    if(cardA.getValue.getRank() > cardB.getValue.getRank())

Similar Threads

  1. Creating a card class for a Deck of cards
    By adjit in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 11:20 PM
  2. Help with creating a deck of cards
    By Carouselification in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 05:45 AM
  3. Creating a deck of cards and printing in console
    By Danieldcc in forum New To Java
    Replies: 7
    Last Post: 10-12-2010, 08:37 AM
  4. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 06:47 PM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts