【问题标题】:VSC Cannot switch on a value of type String for source level below 1.7VSC 无法为低于 1.7 的源级别打开字符串类型的值
【发布时间】:2018-05-13 07:38:05
【问题描述】:

我的代码中有一个错误,因为我使用的是 Switch case 之类的字符串:

private static void setNumberValues(Employee emp, Address address,
        String keyName, long value, List<Long> phoneNums) {
    switch(keyName){
    case "zipcode":
        address.setZipcode((int)value);
        break;
    case "id":
        emp.setId((int) value);
        break;
    case "phoneNumbers":
        phoneNums.add(value);
        break;
    default:
        System.out.println("Unknown element with key="+keyName);    
    }
}

我在使用 Red Hat Java 扩展的 Visual Studio Code 中遇到此错误。

我知道错误意味着什么,这是我的环境:

我的 jdk 版本:

    /usr/libexec/java_home -V
    Matching Java Virtual Machines (1):
    1.8.0_131, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

    /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

我的 Visual Studio Code java.home 设置:

"java.home": "/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home"

如何告诉 VSC 使用我的 jdk 1.8 进行编译,而不要使用旧的 java 1.6?

问候,

S.

【问题讨论】:

  • 您使用的不仅仅是普通的 jdk 吗?喜欢 maven 还是 gradle?
  • 我正在为我的 Visual Studio Code 项目使用 maven。
  • stackoverflow.com/questions/18487406 对此进行了介绍。特别是关于在“build.gradle”中设置源和目标级别的答案。

标签: java visual-studio-code vscode-settings


【解决方案1】:

好的,感谢您的建议,我按照@Stephen-c 的建议阅读了该主题,但实际上这并不能解决我的问题。 我像这样更改 POM:

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

但在 Visual Studio Code 中,我仍然有 keyName 用红色下划线和错误:

[Java] Cannot switch on a value of type String for source level below 1.7. 


Only convertible int values or enum variables are permitted
    String keyName - com.journaldev.json.EmployeeJSONParser.setNumberValues(Employee, Address, String, long, List<Long>)

谢谢,

S.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2021-11-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多