Fares K. A.
Fares K. A.

Reputation: 1285

IntelliJ switch statement using Strings error: use -source 7

I'm trying to use IntelliJ (on Mac OS X) to compile some code I wrote using Eclipse. I've attempted to run the following code:

switch (category) {
    case "below 20":
        below20++;
        break;
    case "20 to 29":
        to30++;
        break;
    case "30 to 39":
        to40++;
        break;
    case "40 to 49":
        to50++;
        break;
    case "50 to 59":
        to60++;
        break;
    case "60 to 69":
        to70++;
        break;
    case "70 t0 79":
        to80++;
        break;
    case "80 to 89":
        to90++;
        break;
    case "90 to 99":
        above90++;
        break;
    default:
        break;
}

However, I get the error:

java: strings in switch are not supported in -source 1.6
  (use -source 7 or higher to enable strings in switch)

I know that some of the answers will be: RTFE (i.e. replace Manual with Error). But I did. I've attempted the following:

And it hasn't fixed it. What am I doing wrong?

Note: I know this is obvious because the directory is above, but I do have Java 7 installed.

Thanks in advance!

Upvotes: 11

Views: 9510

Answers (3)

Jasper van Ams
Jasper van Ams

Reputation: 216

In case anyone still gets this error message after following the advice to check the File > Project Structure -> Project and set appropriate Project language level.

Be sure to also check File > Project Structure> Modules, to see if all your modules are on the correct Language Level: (or the specific desired level).

There is also one more place where this can go wrong, which is: File > Settings > Compiler > Java Compiler. There you can set the project and module's java compiler version.

(For IntelliJ version <= 12 : Project Structure => Project Settings)

Upvotes: 20

jbx
jbx

Reputation: 22158

Remember that using Strings in switch statements is only supported from JDK 1.7 onwards. The default project settings of IntelliJ might not be using JDK 1.7 as default. Change them from the project settings to JDK 1.7.

Upvotes: 0

nikis
nikis

Reputation: 11234

At the Intellij IDEA you should also go to Project Settings -> Project and set appropriate Project language level there

Upvotes: 17

Related Questions