1

I have a problem regarding how can I put an integer value as well as a string in an enum class. I'm using the values of the enum to be stored in the database. What I want is something like this

public enum MyList {
    1,
    2,
    3,
    APPLE,
    BANANA
}

but i have an error in the value '1', it says

Syntax error on token "1", Identifier expected

I've searched for answers here but what I've found is they try to get the value of the enum based on the input integer from the user. What I need is to directly store the values 1,2, 3, APPLE and BANANA in the database.

Is it even possible to combine an integer and a string in an enum? Any help or suggestions would be a great help. Thanks a lot!

PS. I'm very new to programming, so it's quite hard for me to explain the problem using the correct terms. Kindly reply using simple terms! Thank you!

EDIT:

If 1 is the only integer at the said class, the error is like what I've said above. But after I added 2 and 3, it has a red line in public enum MyList { and says

Syntax error on token(s), misplaced construct(s)
chiliflavor
  • 75
  • 1
  • 10
  • 2
    Use ONE,TWO and THREE. I hopw this post may help you http://stackoverflow.com/questions/3990319/storing-integer-values-as-constants-in-enum-manner-in-java – Kajal Apr 27 '16 at 05:35
  • What you're trying to do is not possible. Enums are objects, not integers or strings. Like any objects, they can optionally *wrap* other values. But you'll need to be more specific about what you need to get a helpful example. – shmosel Apr 27 '16 at 05:36
  • @Kajal I'll check it! Thanks! – chiliflavor Apr 27 '16 at 06:31
  • @shmosel noted! Thanks! – chiliflavor Apr 27 '16 at 06:31

5 Answers5

4

As described in Oracle's documentation,

An enum type is a special data type that enables for a variable to be a set of predefined constants.

The names of the constants must satisfy the same rules as any variable, therefore it cannot be an integer.

The style convention is to write constants as uppercase with words separated by underscores.

joel314
  • 1,060
  • 1
  • 8
  • 22
4

You can create a custom enum type like below ,

public enum MyList{

    ONE(1), TWO(2), THREE(3), APPLE("apple"), BANANA("banana");

    Object myItem;

    private MyList(Object myItem) {
        this.myItem= myItem;
    }

 public static MyList customValueOf(Object value) {
        for(MyList v : values())
            if(v.myItem.equalsIgnoreCase(value)) return v;
        throw new IllegalArgumentException();
    }

}
Sagar
  • 818
  • 1
  • 6
  • 13
2

Enums are special data types which contains only constant values in upper case likepublic enum myList{ APPLE, BANANA }

So all these constants are variable names which cannot start a name with number(like 1APPLE as var name, a variable can start with an alphabet or underscore). Remember how variables can be named in java.

BhanuReddy
  • 74
  • 10
2

You might override the toString() for individual enum values that need it, like this:

public enum MyList {
    E1 { @Override public String toString() { return "1"; } },
    E2 { @Override public String toString() { return "2"; } },
    E3 { @Override public String toString() { return "3"; } },
    APPLE,
    BANANA
}

The downside is that you would always have to use myEnumVal.toString() to get the string representation of the enum instead of myEnumVal.name(). If you need String to MyList conversions, you might add a static function like this:

public static MyList fromValue(String v) {
    for (MyList e: MyList.values()) {
        if (e.toString().equals(v)) {
            return e;
        }
    }
    throw new IllegalArgumentException(v);
}
Hank D
  • 6,271
  • 2
  • 26
  • 35
1

Enum is the kind of class with the limited number of its instances named with capital letters. Write the numbers with words out instead:

public enum MyList {
    ONE,
    TWO,
    THREE,
    APPLE,
    BANANA
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183