Case insensitive matching in Java switch-case statement

If you want to do that: just make sure the input data is in all lowercase, and use lowercase cases...

switch ("UPPER".toLowerCase()) {
case  "upper" :

....

Localization issues

Also, the ages old issue of localization strikes again, and plagues this thing too... For example, in the Turkish Locale, the uppercase counterpart of i is not I, but İ... And in return, the I is not transformed to i, but a "dotless i": ı. Don't underestimate this, it can be a deadly mistake...


You try making everything uppercase or lowercase

String str = "something".toUpperCase();
switch(str){
case "UPPER":
}

or

String str = "something".toLowerCase();
swtich(str){
case "lower":
}

or even better use enum (note this is only possible from Java 7)

enum YourCases {UPPER1, UPPER2} // cases.
YourCases c = YourCases.UPPER1; // you will probably get this value from somewhere
switch(c){
case YourCases.UPPER1: ....
break;
case YourCases.UPPER2: ....
}

When using a switch statement you must use "break;" for it to exit the statement, so simply use two cases, one without a break.

switch(choice)

        {

            case 'I':

            case 'i':

                //Insert a name

                System.out.print("Insert a name to add to the list: ");

                input.nextLine();

                name = input.nextLine();

                nameList.insert(name);

                System.out.println();

                break;

This way, if either "I" or "i" are entered, both cases will have the same outcome.