# [SOLVED] Java - convert user input to enum



## Special2God (Oct 5, 2009)

I've been working with enumerated classes in some recent projects, but I always encounter the problem of converting an int or string to an enum. 
I am NOT having trouble doing this, for int I simply:

```
/** Convert integer value to enum MyEnum
	 * @param convert - int value to convert to MyEnum enum
	 * @return A enum MyEnum value, returns UNDEFINED "0" if the convert value is 0 or if the value is not valid
	 */
	public static MyEnum convert(int convert) {
		if(convert > -1 && convert < MyEnum.values().length) {
			return MyEnum.values()[convert];
		}
		else {
			return MyEnum.UNDEFINED;
		}
	} // End convert method
```
Strings can also be done (I saw an example online), but I won't include the example. 

So, my problem is that rl people and internet forums, blogs, etc... are saying that you should never ever convert an int or string to an enum. 
I have not gotten a clear reason as to why, other than that it violates the enum concept, which I am 
sorry about, but see no way around, since there are no .nextEnum() methods for reading input. 
I ran across a forum somewhere where a user had a similar question and got the answer: a map. (Which I assume is some java concept)...

Any help as to how to properly convert user input (int or string) to enum values would be appreciated?


----------



## Ninjaboi (Dec 27, 2010)

*Re: Java - convert user input to enum*

Hello Special2God.

In all honesty, you can do whatever makes it easier for you to do your tasks. The whole 'concept' behind enums typically runs with working with constant values, as well as more structured reasons.

Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Of course you can do more than constants, but that's what it's used for most of the time. Not sure what 'a map' means, but it's useless jargon unless you find the exact topic it's supposed to encapsulate. The reason why people tell you it defeats the purpose of converting a value to an enum is that they are tailored specifically for constant values that are related to each other. Converting another variable into an enum is basically creating a constant later in the game than needed ( as constants should be declared either in the pre-processor or in early parts of your code ). 

Just stick with your guns, and do what you want. As far as I know, there isn't any "Programming police" invented *YET!*.

Happy coding!


----------



## Special2God (Oct 5, 2009)

*Re: Java - convert user input to enum*

Thanks for the reply Ninjaboi, 
In almost every case int convert is a user entered value, it can never be decided before run time. 
I agree, enums by nature are suited to be used as constants. 


> ... do what you want. As far as I know, there isn't any "Programming police" invented *YET!*.


lol, that's what I find in programming. The lack of programing "crimes" can be helpful when I program, but detrimental when working with other programmer's code. 

On a different note, what methods have you seen other people use or you yourself have used 
to get and store user input that the user choose from a list of options in a java console program? 
(if it were a GUI I would just use some type of list or drop down box to get the user's selection) 
TY


----------



## Ninjaboi (Dec 27, 2010)

*Re: Java - convert user input to enum*

I usually just create a variable at the beginning of the program, and then use a switch for the different options they can input in validly ( I put this inside a do-while loop that only lets you out after a valid entry ). The input gets stored in the variable I created ( I usually use integers that number off each option ).

If you wish to see some code examples, feel free to ask.


----------



## Special2God (Oct 5, 2009)

*Re: Java - convert user input to enum*

Thank you for the info, I think that answers my current question. 
I'll mark this thread as solved and post a new tread if I have any other questions. 
:wave:


----------



## Ninjaboi (Dec 27, 2010)

Great, glad it helped!

Happy coding!


----------

