Suppose I have an enum structure like the following
public enum MyItems {
pc("macbook"),
phone("nokia"),
food("sandwich"),
animal("dog");
private String item;
MyStuff(String stf) {
item = stf;
}
public String identify() {
return item;
}
}
Now, suppose I want to find out what "type" an item I own is. For instance, in the case of "macbook", this would yield pc. Similarly, "sandwich" would represent a food item.
I have the following for-loop for checking if a String belongs to an enum:
String currentItem; //Some arbitrary string, such as "macbook"/"nokia", etc.
for(MyStuff stuff : MyStuff.values()) {
if(stuff.identify().equals(currentItem)) {
//PRINT OUT:
//currentItem + " is a " + pc/phone/food/animal
}
}
That is, how can I go from taking the parameter of an enum value, to the enum value "type" it represents.
This is the desired output:
currentItem = "nokia"
>>>> nokia is a [phone]