java 无法在 JRE7 中打开字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7840163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:38:56  来源:igfitidea点击:

Unable to switch on String in JRE7

javaswitch-statement

提问by amnesyc

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?

我在 jre7 上,但仍然无法打开字符串。我安装了 jdk7 update 1 并将 Eclipse 指向它,但仍然没有运气。知道我做错了什么吗?

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.

无法打开字符串类型的值。只允许使用可转换的 int 值或枚举常量。

回答by MTilsted

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

我很确定 Eclipse 3.7 只支持 jdk 1.6。你需要 3.7.1 根据 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).

(记住:Eclipse 不使用 suns 编译器,他们使用自己的,所以安装 jdk 1.7 是不够的)。

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

只需打开 eclipse 并选择帮助-> 检查更新。- 那应该将你升级到 3.7.1

回答by monksy

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.

听起来您仍在尝试在旧 JDK [6 或更早版本] 下编译 安装 JDK,并确保您的 Eclipse 设置反映新平台。另外检查您的项目是否反映了 JDK7 平台。

回答by Michael Berry

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.
  • 确保 Eclipse 不仍然指向旧的 (6) JDK 安装。
  • 确保您没有选择较低的源代码级别进行编译;例如。- 来源 1.6。
  • 确保您使用的是支持 Java 7 语法的 Eclipse 版本。