IntelliJ switch statement using Strings error: use -source 7

扶醉桌前 提交于 2019-11-30 23:38:43

问题


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!


回答1:


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




回答2:


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)




回答3:


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.



来源:https://stackoverflow.com/questions/22481103/intellij-switch-statement-using-strings-error-use-source-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!