Hello I seem to be having trouble with a project that I am working on. I have created an enum type named Shift for breakfast lunch dinner swing), I then have a switch with 4 cases to choose which one it is.
However when I try to create an object without any user input, and set the value to a random number from 1-4, there is a message that appears saying "incompatible types : int can not be converted to Shift"
any help would be appreciated!
EDIT* I currently have this
private void getShiftAsInput()
{
Scanner cin = new Scanner(System.in);
System.out.print(" Please enter " + getName()
+ "'s Shift by Number:\n"
+ "1. Breakfast\n"
+ "2. Lunch\n"
+ "3. Dinner\n"
+ "4. Swing ========> ");
String inShift = cin.nextLine();
switch(inShift){
case "1": shift = Shift.breakfast; break;
case "2": shift = Shift.lunch; break;
case "3": shift = Shift.dinner; break;
case "4": shift = Shift.swing; break;
default: getShiftAsInput(); break;
}
}
public enum Shift { breakfast, lunch, dinner, swing }
and in the main classs am trying to do this:
RestaurantWorker[] arr;
arr = new RestaurantWorker[6];
arr[0] = new Manager();
arr[1] = new Manager("John Edelman", "8521100900", 3, 10, 1000, 40000);
arr[2] = new WaitStaff();
arr[3] = new WaitStaff("Kelsey Green", "5662902901", 3, 36, 10.25, 40, 10, 2 );
arr[4] = new KitchenStaff();
arr[5] = new KitchenStaff("Jeff Brown", "8596042909", 2, 30, 10, 30, 0);
but am getting an error for the argument after the phone number.
You have to provide a way to convert int
to your target Enum
.
This is my way to do that:
public enum Shift {
BREAKFAST(1), LUNCH(2), DINNER(3), SWING(4);
private final int value;
public Shift(int value) {
this.value = value;
}
private static final Map<Integer, Shift> SHIFT_MAP = new HashMap<>();
static {
Arrays.stream(Shift.values()).forEach(s -> SHIFT_MAP.put(s.getValue(), s));
}
public static Shift of(Integer value) {
return SHIFT_MAP.getOrDefault(value, BREAKFAST); // Here you provide a default Shift value if input wrong value
}
}
And in your switch statement:
Shift shift = Shift.of(value);
switch(shift) {
case BREAKFAST:
... bla bla
}
This is how your code should be, the type mismatch is because you have to pass ENUM into the switch case.
public class Practise {
static enum SHIFT {
BREAKFAST, LUNCH, DINNER
}
public static void main(String[] args) {
SHIFT key = SHIFT.BREAKFAST; // hardcoded for example, but can be dynamic
switch (key) {
case BREAKFAST:
break;
default:
break;
}
}
}
When you generate random integer values, you can change your logic to assign an ENUM value to the variable. Something like,
SHIFT key = SHIFT.values()[getRandomIndex()];
You're setting an an int type to an Object Type, instead of "Shift = int" try "Shift.YourDataField = int"
"YourDataField" is whatever field you set in your object, and it should be an int type
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.