0

I have an error in my code because I am using a String like a 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);    
    }
}

I am facing this error in Visual Studio Code with the Red Hat Java extension.

I know what the error means so, here is my environment:

My jdk version:

    /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

My Visual Studio Code java.home settings:

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

How to tell VSC to compile with my jdk 1.8 and don't bother with old java 1.6 ?

Regards,

S.

Stefano Bossi
  • 1,138
  • 1
  • 9
  • 19

1 Answers1

1

ok thanks for the suggestions, I read the thread as suggested by @Stephen-c but actually this doesn't solve my problem. I change the POM like this:

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

But in Visual Studio Code I still have the keyName underlined in red and the error:

[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>)

Thanks,

S.

Stefano Bossi
  • 1,138
  • 1
  • 9
  • 19