Ant_Magma
07-19-2005, 11:37 AM
import java.util.Scanner;
class EnumSwitchDemo{
enum Flavor {VANILLA, CHOCOLATE, STRAWBERRY};
public static void main(String[] args){
Scanner keyboard=new Scanner(System.in);
System.out.println("What is your favourite flavor?");
System.out.println("Vanilla, Chocolate or Strawberry?");
String answer=keyboard.nextLine();
answer=answer.toUpperCase();
Flavor favourite=Flavor.valueOf(answer);
//Returns the enum constant of the specified enum type with the specified name.
switch(favourite){
case VANILLA:
System.out.println("Classic");
break;
case CHOCOLATE:
System.out.println("Rich");
break;
default:
System.out.println("I bet you said STRAWBERRY");
break;
}
}
}
When i try to enter anything other than vanilla, choc n strawberry i get an exception. How do i code it so that when anything other than those cases is entered, it'll print d default?
class EnumSwitchDemo{
enum Flavor {VANILLA, CHOCOLATE, STRAWBERRY};
public static void main(String[] args){
Scanner keyboard=new Scanner(System.in);
System.out.println("What is your favourite flavor?");
System.out.println("Vanilla, Chocolate or Strawberry?");
String answer=keyboard.nextLine();
answer=answer.toUpperCase();
Flavor favourite=Flavor.valueOf(answer);
//Returns the enum constant of the specified enum type with the specified name.
switch(favourite){
case VANILLA:
System.out.println("Classic");
break;
case CHOCOLATE:
System.out.println("Rich");
break;
default:
System.out.println("I bet you said STRAWBERRY");
break;
}
}
}
When i try to enter anything other than vanilla, choc n strawberry i get an exception. How do i code it so that when anything other than those cases is entered, it'll print d default?