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)
4
  • 2
    Use ONE,TWO and THREE. I hopw this post may help you stackoverflow.com/questions/3990319/… Commented Apr 27, 2016 at 5: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. Commented Apr 27, 2016 at 5:36
  • @Kajal I'll check it! Thanks! Commented Apr 27, 2016 at 6:31
  • @shmosel noted! Thanks! Commented Apr 27, 2016 at 6:31

5 Answers 5

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.

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

Comments

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();
    }

}

Comments

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.

Comments

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);
}

Comments

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
}

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.