I have the following Enum:
public enum Food {
APPLE,
BREAD,
PIE,
STEAK;
}
I want to store the user's chosen food like so Food food = scanner.next()
(obviously this is incorrect). What would be a good way to do it?
I have the following Enum:
public enum Food {
APPLE,
BREAD,
PIE,
STEAK;
}
I want to store the user's chosen food like so Food food = scanner.next()
(obviously this is incorrect). What would be a good way to do it?
You can do the following:
Scanner scanner = new Scanner(System.in);
Food food;
String input = scanner.next();
for (Food item : Food.values()) {
if(input.equals(item.name())) food = item;
}