38

I'm developing an android app on eclipse, when I try some code like

switch(String){
 ......
}

it give me the following error

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

so I changed the compiler to 1.7, then it give me another error

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.

So, is there anyway to let switch(String) works in android application? or I have no choice but to use

if(){}
else if(){}
else if(){}
........
1

6 Answers 6

94

With the Release of API 19 KitKat you can now do this !!!! This is the change you need for android studio for existing projects. ensure that you are using JDK 1.7 or later and Gradle plugin 0.6.1 or later

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

defaultConfig {
    minSdkVersion 7
    targetSdkVersion 19
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
} }
Sign up to request clarification or add additional context in comments.

3 Comments

@Eddwhis yes, it will still work! Source: Tested switch ("test") { case "test": Log.e("Yay", "It Works"); break; } on API 18!
So is it the compileOptions which are the key thing here?
@bclymer You're partially right. It truly does work below API level 19, because it's about compiler behavior and not about device behavior. However, Your test proves nothing, because of Java optimalization. Compiler may try to reduce memory usage by reusing String objects in literals - so your test may or may not pass even before Java 1.7.
38

switch statement on String objects is a new feature introduced in Java 1.7. Unfortunatelly Android requires version 1.6 or 1.5. This is why you have to forget for some time about such constructions.

You can avoid using if-statements-chain by storing the map of methods which will be executed for certain String: Map<String, Method>. You can always encapsulate Method it with some Handler object. Look here for more info: How to remove large if-else-if chain

and

why-cant-i-switch-on-a-string

1 Comment

This answer is deprecated with release of KitKat. Check latest answer
6

You have no choice. You can't work Switch with Strings (at least for now). Following message explicitly conforms that.

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead

Comments

5

You can do this with the latest android build tools. Android started supporting Java 7 feature since API 19.

The best part is that it will work even if you set your minSdkVersion lower than 19. Quoting this android developer page:

Note that you can use minSdkVersion with a value earlier than 19, for all language features except try with resources. If you want to use try with resources, you will need to also use a minSdkVersion of 19.

Comments

3

Here is other alternative for android with java 5 or 6

public enum Command  {text, image, other};

String enumTest = "text";
try{
    Command command  = Command.valueOf(enumTest);
}
catch(Exception e){
    Log.e("Tag", "Command not found: " + enumTest);
}
switch (command){
   case image:
      break;
   case  text:
       break;
}

Comments

1

Since I assume most people are using Android Studio with at least a minimum of Java 7, then the answer now is, Yes, you can switch Strings in Android, even for minSdkVersion versions < 19. Here is an example of how to do it, taken from the Java SE Documentation:

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
     String typeOfDay;
     switch (dayOfWeekArg) {
         case "Monday":
             typeOfDay = "Start of work week";
             break;
         case "Tuesday":
         case "Wednesday":
         case "Thursday":
             typeOfDay = "Midweek";
             break;
         case "Friday":
             typeOfDay = "End of work week";
             break;
         case "Saturday":
         case "Sunday":
             typeOfDay = "Weekend";
             break;
         default:
             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
     }
     return typeOfDay;
}

The docs also note:

  • Using string switching is generally faster than chained if-else.
  • It is as if each case were using String.equals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.