1

I'm trying to use a switch statement in a Android Application. This switch statement is testing the value of a String, and then based of that changing the value of another String.

String s1 = "a" then String s2 = "1"

I keep getting an error that says I need to change the compliance to 1.7. After I do that, I then get an error that says I need to change the compliance to 1.6.

Is there a way to fix this? or can anyone think of a work around for this?

6
  • Can you post your code please? Commented Dec 29, 2013 at 20:06
  • You can't switch on strings pre-java 7. What Java version do you have? And what is your code exactly? Commented Dec 29, 2013 at 20:08
  • stackoverflow.com/questions/338206/…. I guess android still does not support java 7 Commented Dec 29, 2013 at 20:09
  • Before Java 7 (not yet supported by Android) you can use switch only to compare integers... :( This is not Visual Basic! Commented Dec 29, 2013 at 20:16
  • tools.android.com/tech-docs/new-build-system/…. As of build tools 19 java 7 is supported Commented Dec 29, 2013 at 20:29

4 Answers 4

7

Java does not support String in switch/case earlier java 7. But if you use java 6 you can achieve the desired result by using an enum.

private enum Fruit {
apple, carrot, mango, orange;
}

String value; // assume input
Fruit fruit = Fruit.valueOf(value); // surround with try/catch

switch(fruit) {
    case apple:
        method1;
        break;
    case carrot:
        method2;
        break;
    // etc...
}
Sign up to request clarification or add additional context in comments.

5 Comments

Enum constants should be UPPERCASE, as they are essentially public static final fields.
you are right, but i don't say how to programming, i give an idea how to solve this problem
check my question if it help you
while i didn't use this method, it did get to thinking: since I'm only looking for one letter that a user enters (I was only using string cause I found it to be easier) I could just convert the letter to it's ASCII value and base my switch statement off that.
@Just_Some_Guy Why not use the char itself?
1

Using String comparison with equals() instead of switch on Strings (which is available in Java 7) could look like this:

//as an example, if s2 equals 'b'
String s2 = "b";

//this uses String's equals() method
if (s1.equals("a")) then {
    s2 = "1";
}
else if (s1.equals("b")) then {
    s2 = "2";
}
else {
    s2 = "3";
} 

Of course, adapt the conditions to your needs.

1 Comment

Thanks! Im going to try this out next chance I get.
0

donot use switch case but rather use if-else with string equals() method. it will help in code compatibility as switch with strings is only available with jdk 1.7+.

2 Comments

this was also giving me headaches when using strings, though I'm not sure what is meant by "string equals method()". Could you give a quick example of this?
sorry, I'm still ultra new at this.
0

You will need to add the below code in your pom.xml if in case it is a maven project.

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

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.