1

Hi Android Developers,

I have a problem with string splitting in android in tells that change the java compliance to JRE 1.7 when i changed the compliance error occurs because it works only in api level 19

   String st="a,b,c,d";
   String[] temp=st.split(",");
   for(int i=0;i<temp.lenght;i++)
   {
       switch(temp[i])
       {
           case "a":
                   //print something
                   break;
           case "b":
                  //print something
                  break;
           case "c":
                 //print something
                  break;
           case "d":
                 //print something
                 break;
          default:
                 //print something
       }
   }

The error is in the switch statement, how can I solve it I am developing in 2.3 devices?

The error:

[2014-08-22 11:47:26 - text] Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8
Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42
Ribin Haridas
  • 1,630
  • 11
  • 17
  • 1
    possible duplicate of [switch for type string for java 1.6](http://stackoverflow.com/questions/15533534/switch-for-type-string-for-java-1-6) – Scary Wombat Aug 22 '14 at 06:12
  • 1
    Switching strings is implemented in Java 1.7+ For the API lvl 19 error - i don't see here any Android specific code. – localhost Aug 22 '14 at 06:13
  • 2
    Just replace your switch statement with set of else-if statements Switch by String was introduced in JDK 1.7 while Android SDK based on JDK 1.6 – Viacheslav Aug 22 '14 at 06:15
  • You haven't defined anything in the `default` case. – gosr Aug 22 '14 at 06:15
  • 1.6 doesn't allow you to pass strings in switch statement. Please change it. – Aniruddha Aug 22 '14 at 06:18
  • possible duplicate of [Why can't I switch on a String?](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string) – Peter O. Aug 22 '14 at 15:53

3 Answers3

3

To resolve: Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8

  1. Try Project --> Properties --> Java Compiler --> Compliance Level --> 1.6

    Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8

  2. After setting compliance level, clean the project. If required, restart eclipse.

Also, as James pointed out, your for loop should be i<temp.length (Spelling mistake should be corrected)

ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • @Ribin. Thank you for accepting as answer. Please also upvote since it would increase our tag scores. TIA! – ngrashia Aug 22 '14 at 07:06
2

You have made a spelling error:

for(int i=0;i<temp.lenght;i++)
                    ^^

It should say length not lenght

James
  • 338
  • 1
  • 2
  • 16
0

The Compiler compliance level is Version Dependent in eclipse. If you are using Eclipse 3.7 or lower then you won't get 1.7 compliance. Use Eclipse JUNO or higher.

For more on JDT Core 7 and its release related information

UPDATE:

Even eclipse 3.7.1 version supports Java 7 features and compliance level.

Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42