1

I recently decided to start using enum for my card Suit however it seem that I can't resolve this error no matter how I type up the code.

Edited:

/**
 * 
 * A basic constructor class that build the card object. 
 * The card object will contain value for both the Suit and 
 * card's Value.
 * 
 * 
 *
 */
public class Card {

    public Enum SUIT{SPADE, CLUB, DIAMOND, HEART};

    // This will identify the card suit.
    public Enum suit; 
    //This will hold the card value.
    //Jack = 11 ... Ace = 14
    public int value;


    public Card(Enum suit, int value){

        this.suit = suit;
        this.value = value;
    }
}

I have look up various answer on the site and look through the official Java Documentation on Orcacle. Can someone help me figure this out?

Update:

/**

* * A basic constructor class that build the card object. * The card object will contain value for both the Suit and * card's Value. * * * */ public class Card {

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

// This will identify the card suit.
private final Suit suit; 
//This will hold the card value.
//Jack = 11 ... Ace = 14
private final int value;


public Card(Suit suit, int value){

    this.suit = suit;
    this.value = value;
}

}

Here a screenshot of the errors: A screenshot of the error

  • Please don't change your question on the basis of answers. It makes it hard to understand the answers if they recommend things which are already present in the question. If you need to, add updates to the question underneath. – Andy Turner Dec 23 '15 at 11:30
  • The code you currently have will not give you an error like "SPADE cannot be resolved to a type". Please add the code where this error occurs. – Andy Turner Dec 23 '15 at 11:31
  • Ah sorry still new to this but learning. Added the error and revert the original code back and added the updated. – J Scott Anderson Dec 23 '15 at 22:30
  • The code you've got in your latest edit is not the same code that is shown in your image. Which code are you asking us to look at? Also, instead of linking to an external image-hosting service, simply [add your image directly to your post](http://meta.stackexchange.com/questions/75491/how-to-upload-an-image-to-a-post). – Klitos Kyriacou Dec 23 '15 at 22:54
  • @KlitosKyriacou sorry about that been copying and pasting from pc to phone, it fixed now. I'm focusing on the updated one as that is the correct declaration. However I'm getting the error that shown in the image. Ah I did but it said I been at least 10 rep to do so. So I opt for a more roundabout way. – J Scott Anderson Dec 23 '15 at 23:03
  • For one thing, it should be `Suit`, not `SUIT`. Java identifiers are case-sensitive. – Andy Turner Dec 24 '15 at 06:36

4 Answers4

2

Enum is a class name (it is the base class of all enums). You need to declare the Suit class as:

public enum Suit{SPADE, CLUB, /* etc */}

Then, in order to accept an instance of Suit as a parameter to your constructor, make the parameter type Suit, as well as the corresponding field:

public Suit suit;
public int value;

public Card(Suit suit, int value) {
  this.suit = suit;
  this.value = value;
}

A couple of other observations:

  • Make your suit and value fields private, and add accessor methods (getSuit(), getValue()). Currently, nothing stops somebody outside the class from changing them.
  • Also, consider making them final, unless there is a good reason to want to be able to change them later inside this class, or you want to expose setters for them (I can't imagine what that reason would be)
  • Check the range of value is between 2 and 14; alternatively, create an enum for the card values. It's a little bit of one-off typing, but it will lead to much more descriptive code (e.g. you can use Value.ACE instead of wondering if you should use 1 or 14 to represent that card)
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

If you want to define an enum type

// lowercase
public enum SUIT{SPADE, CLUB, DIAMOND, HEART};

If you want to have a variable SUIT

SUIT suit;
  • I have done the lowercase enum but it would then say that enum cannot be resolve to a type. – J Scott Anderson Dec 23 '15 at 09:54
  • In Java there is no ``Enum'' only enum. You have to change ``public Enum'' suit to ``public SUIT'' suit and your parameter ``Enum suit'' to ``SUIT suit'' – 65-e9-7e-17 Dec 23 '15 at 09:56
  • Like this: public Suit suit; public Card( Suit suit, int value) {...}? – J Scott Anderson Dec 23 '15 at 10:05
  • @65-e9-7e-17 [`Enum` does exist in Java](http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html): it is the class which all `enum` classes implicitly extend. It's just not a keyword. – Andy Turner Dec 23 '15 at 10:08
0

enum is the right keyword you are looking for.

UPDATE

To make your code work,change Suit(mostly a typo) to SUIT(enum declared by you)

public class Card {

    public enum SUIT{SPADE, CLUB, DIAMOND, HEART};

    // This will identify the card suit.
    private final SUIT suit; 
    //This will hold the card value.
    //Jack = 11 ... Ace = 14
    private final int value;


    public Card(SUIT suit, int value){

        this.suit = suit;
        this.value = value;
    }
}
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • Why introduce the complication of a value associated with each SUIT value? OP's question doesn't require that: each Card has a value, not each Suit. – Andy Turner Dec 23 '15 at 10:13
0

Your latest edit shows a screenshot containing this code:

public class Card {
    public enum Suit {SPADE, CLUB, DIAMOND, HEART};

    public final SUIT suit;

Your enum declaration is fine. The fact that you are getting the error message "enum cannot be resolved to a type" suggests that your compilation settings are targetting an obsolete version of Java. Enums did not exist before Java 1.5.

Make sure you are using the latest Java compiler (1.8) and then go into Eclipse properties for your project. Click on Java Compiler. Under JDK Compliance, make sure it is set to 1.5 or later (preferably 1.8).

Your next error is the fact that SUIT != Suit. Be careful with the case of your letters.

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • Ok that work thank!!!! But now I notice that I don't have 1.8 even through I recently install it on my computer and set the build path up. Is there something I must do on eclipse to make it show up? – J Scott Anderson Dec 23 '15 at 23:25
  • Make sure you've set the PATH environment variable for jdk1.8, following [Oracle's instructions](https://docs.oracle.com/javase/8/docs/technotes/guides/install/windows_jdk_install.html#BABGDJFH). Then, set (or create) the environment variable JAVA_HOME to point to your JDK directory (e.g. C:\Program Files\Java\jdk1.8.0_66). Finally, point Eclipse at the latest jdk (see, for example, [this SO question](http://stackoverflow.com/questions/13635563/setting-jdk-in-eclipse).) – Klitos Kyriacou Dec 24 '15 at 00:45
  • I set up the JAVA_HOME variable but haven't found any instruction as what to do with it. The linked question above didn't say much about it. Can you give a bit of advice on what to do with it? – J Scott Anderson Dec 24 '15 at 09:29
  • It's not always necessary to define JAVA_HOME, but it can be useful as there are some software products such as IDEs, build tools, and other applications, that use the JAVA_HOME environment variable to find the jdk directory. – Klitos Kyriacou Dec 24 '15 at 09:46