r/javahelp • u/BrutalBananaW • Sep 30 '19
Quick beginner question: let’s say I scan the users answer to “what is your gender?” A) Male B) Female How do I store A as “male”?
I want to follow up with a question: Gender: Male Is this correct?
23
Upvotes
1
u/mk321 Sep 30 '19
You can create enum with values (Strings):
And check it in this way:
Or you can write your own method
valueOfStringin your enum (becausevalueOfin Java based on enum name:Gender.valueOf("MALE")so you lose the advantage of values in enums and have to uppercase input):And you can use it simply: