Reputation: 172
I've class Aligment like below:
public enum Aligment
{
Evil,
Neutral,
Good,
Undefined
}
And I want to use these values in switch like so:
System.out.print("Choose you'r start up character" +
"1.Good" +
"2.Evil" +
"3.Neutral");
//1 string alignmentChoice = scan.nextLine();
//2 Aligment alignmentChoice = Aligment.Undefined;
switch( aligmentChoice )
{
case Good:
alignment = Aligment.Good;
break;
case Evil:
alignment = Aligment.Evil;
break;
case Neutral:
alignment = Aligment.Neutral;
break;
default:
System.out.println("How did you manage to get here? You have broke the system.");
break;
}
And I'm not sure how to use it like //1 or //2. Thanks for help in advance.
Upvotes: 4
Views: 170
Reputation: 17880
Create a mapping (static map) inside your enum for holding the map between the name to be provided by the user to the enum.
public enum Alignment {
Evil("Evil"),
Neutral("Neutral"),
Good("Good"),
Undefined("Undefined");
private static final Map<String, Alignment> MAPPINGS = new HashMap<>();
static {
for (Alignment alignment : Alignment.values()) {
MAPPINGS.put(alignment.getName(), alignment);
}
}
private String name;
Alignment(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Alignment getAlignmentForName(String name) {
return MAPPINGS.get(name);
}
Now, you can use getAlignmentForName
to map the user input to an enum. It will return null
for invalid values.
The advantage of this is that you need not change any code when you add a new enum instance... In your case, an appropriate switch case has to be added.
Note: The names of enum fields must be in uppercase letters as per the conventions.
Upvotes: 0
Reputation: 1384
If you refactor and move this switch case logic to a method in your enum, you no longer need to use a switch statement for getting an enum from a string.
In the following example, I have added a method fromString()
which will take a string input name
, and compare with all of our enum values (case unsensitive).
If a matching value is not found, we throw an IllegalArgumentException
.
Here is the example:
public class AlignmentTest{
public static void main(String[] args){
String good = "good";
String neutral = "NEUTRAL";
String evil = "EvIl";
String unknown = "unknown";
Alignment alignment1 = Alignment.fromString(good);
System.out.println("Alignment 1: " + alignment1);
Alignment alignment2 = Alignment.fromString(neutral);
System.out.println("Alignment 2: " + alignment2);
Alignment alignment3 = Alignment.fromString(evil);
System.out.println("Alignment 3: " + alignment3);
Alignment alignment4 = Alignment.fromString(unknown);
System.out.println("Alignment 4: " + alignment4);
}
public enum Alignment {
EVIL("Evil"),
NEUTRAL("Neutral"),
GOOD("Good");
private String name;
Alignment(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public static Alignment fromString(String name) {
for (Alignment alignment : Alignment.values()) {
if (alignment.name.equalsIgnoreCase(name)) {
return alignment;
}
}
throw new IllegalArgumentException("No alignment with name " + name + " found");
}
}
}
This outputs the following:
Alignment 1: GOOD
Alignment 2: NEUTRAL
Alignment 3: EVIL
Exception in thread "main" java.lang.IllegalArgumentException: No alignment with name unknown found
at AlignmentTest$Alignment.fromString(AlignmentTest.java:44)
at AlignmentTest.main(AlignmentTest.java:19)
Upvotes: 2
Reputation: 15423
Use it like so :
switch(Aligment.valueOf(alignmentChoise)) {
case Evil:
alignment = Aligment.Evil;
break;
}
Note :
This will throw IllegalArgumentException
if the enum constant is not found.
Upvotes: 4