amnesyc
amnesyc

Reputation: 5371

Unable to switch on String in JRE7

I'm on jre7 and I still can't switch on Strings. I installed jdk7 update 1 and pointed Eclipse to it but still no luck. Any idea what I'm doing wrong?

Here is a the code:

String code = "something";
switch(code) {
    case "xxx": dosomehting(); break;
    default: dosomethingelse(); break;
}

The error I get:

Cannot switch on a value of type String. Only convertible int values or enum constants are permitted.

Upvotes: 3

Views: 12118

Answers (3)

MTilsted
MTilsted

Reputation: 5545

I am pretty sure that Eclipse 3.7 only support jdk 1.6. You need 3.7.1 according to http://download.eclipse.org/eclipse/downloads/drops/R-3.7.1-201109091335/index.php

(Remember: Eclipse don't use suns compiler, they use their own, so installing jdk 1.7 is not enough).

Just open eclipse and select help->Check for updates. - That should upgrade you to 3.7.1

Upvotes: 2

Michael Berry
Michael Berry

Reputation: 72294

Three things spring to mind to check:

  • Make sure that Eclipse isn't still pointing to the old (6) install of the JDK.
  • Make sure that you haven't selected a lower source level for compilation; eg. -source 1.6.
  • Make sure you're using a version of Eclipse that supports Java 7 syntax.

Upvotes: 0

monksy
monksy

Reputation: 14234

It sounds like you're still trying to compile under the old JDK [6 or earlier] Install the JDK, and make sure your Eclipse settings reflect the new platform. Additionally check if your project is reflecting the JDK7 platform.

Upvotes: 1

Related Questions