14

I want to define string and integer together but it gives errors.

public class Card {
    Rank rank;
    Suit suit;
    public Card(Rank rank, Suit suit){
        this.rank=rank;
        this.suit=suit;
    }
    public enum Rank { Ace, 9, Queen, King } //and other suits
}

The error is a syntax error on token 9, delete this token.

2
  • i don't think you can put integer as an enum value Commented Aug 15, 2013 at 8:34
  • A integer is not a valid variable name in Java! Commented Aug 15, 2013 at 8:36

6 Answers 6

10

In declaring enum in Java { Ace, 9, Queen, King } these are not strings and integers. These are actual objects of enum.

You can do this:

 public enum Rank { 
     Ace(13), 
     Nine(8),
     //etc
     ;

     private int rank;

     Rank(int rank) {
         this.rank = rank;
     }

     public int getRank() { 
         return rank;
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

enum Rank { Ace, 9, Queen, King }; gives an error like this Syntax error on token "9", Identifier expected ....i cannot understand why?
@AnudeepSamaiya Because 9 is not an valid variable name. You cannot name your variables 9, enum constants also act like a variable. They even may be, in effect, variables.
9

The things inside enums are identifiers, names of static final (constant) objects that'll be created. So, you can't use an int for naming an object.

enums allow you to place fields for each entry:

public static enum Suit {HEART,DIAMOND,SPADE,CLUB}

public static enum Cards {
    ACE_OF_HEART(Suit.HEART,14), QUEEN_OF_SPADE(Suit.SPADE,13) /* etc etc*/;

    private final Suit mSuit;
    private final int mRank;

    private Cards(Suit suit, int rank) {
        assert rank >= 2 && rank <= 14;
        mSuit = suit;
        mRank = rank;
    }

    public Suit getSuit() {
        return mSuit;
    }

    public int getRank() {
        return mRank;
    }
}

You really don't want to code all 52 cards this way. You can model it another way:

Suite:

public static enum Suit { SPADE, HEART, DIAMOND, CLUB};

Class with some popular ranks as named constants:

public class Card{

  public static final int ACE = 14;
  public static final int QUEEN = 13;
  public static final int KING = 12;
  public static final int JACK = 11;

  private final int mRank;
  private final Suite mSuit;

  public Card(Suite s, int r){
    this.mSuit = s;

    if(r < 2 || r > 14){
      throw new IllegalArgumentException("No such card with rank: "+r);
    }    

    this.mRank = r;
  }

  public Suit getSuit() {
        return mSuit;
    }

    public int getRank() {
        return mRank;
    }
}

Comments

4

You cannot start an enum field with a number. Try using NINE.

Comments

2

Java naming rules say you cannot start variables, class names ect... with numbers.

Comments

1

try

public enum Rank    { Ace, Jack,  Queen, King }//and other suits

instead of your enum

the problem is that if you could have put number as enum value then the compiler won't know, when someone write "9" it he meant the number or the enum. so java, as almost every language, won't allow this kind of enum value

2 Comments

@DuncanJones copy paste from the code that worked that went wrong. fixed and added information
@DuncanJones, i know. thank you btw, i always try to leave at the very least a good working answer
1

U cannot use integers inside enum like this. Please go through this: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Comments

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.