0

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?

1 Answers1

-1

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;

}
Luke Taylor
  • 9,481
  • 13
  • 41
  • 73