Domanda

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:

  • Preferences > Compiler > Java Compiler and added -source 7 and/or -target 1.7 to Additional command line parameters
  • Edit Configurations: checked Use Alternative JDK and wrote in the directory /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home

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!

È stato utile?

Soluzione

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)

Altri suggerimenti

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top