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(){}
........
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
jing1988ccc
  • 571
  • 1
  • 4
  • 10
  • take a look at this http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java It's got some interesting answers – RNJ Jan 16 '13 at 20:54

6 Answers6

93

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
} }
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
  • 5
    @Eddwhis yes, it will still work! Source: Tested `switch ("test") { case "test": Log.e("Yay", "It Works"); break; }` on API 18! – bclymer Apr 16 '14 at 04:22
  • So is it the `compileOptions` which are the key thing here? – Tim Feb 04 '15 at 11:50
  • @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. – prompteus Nov 06 '16 at 08:47
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

Community
  • 1
  • 1
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
  • 28
    This answer is deprecated with release of KitKat. Check [latest answer](http://stackoverflow.com/questions/14367629/android-coding-with-switch-string/19722741#19722741) – mente Jan 08 '14 at 10:17
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

kosa
  • 65,990
  • 13
  • 130
  • 167
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.

Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
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;
}
Juan Giorello
  • 333
  • 2
  • 13
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.
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393