How to use the inputs of string array in switch case?
String[] mon=new String[]{"January","February","March","April","May","June","July","August","September","October","November","December"};
switch (mon)
{
case "January":
m=1;
break;
case "February":
m=1;
break;
}
Java (before version 7) does not support String in switch
case. But you can achieve the desired result by using an enum
.
private enum Mon {
January,February,March,April,May,June,July,August,September,October,November,December
};
String value; // assume input
Mon mon = Mon.valueOf(value); // surround with try/catch
switch(mon) {
case January:
m=1;
break;
case February:
m2;
break;
// etc...
}
Please see here for more info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With