Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use String[] inputs in switch case in Android?

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;                  
}
like image 727
user3739889 Avatar asked Jan 11 '23 10:01

user3739889


1 Answers

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

like image 104
Giru Bhai Avatar answered Jan 21 '23 09:01

Giru Bhai