Java 1.7 still telling me that strings in switch statements are incompatible
Asked Answered
I

5

6

I'm using java 1.7 in intellij and it's giving me a compile error as if I'm using pre-1.7. Saying, "Incompatible type. Found 'java.lang.String'. required 'byte, char, short, or int'.

I'm rattling my brain trying to figure out why this is. Thanks

Insphere answered 10/9, 2014 at 15:6 Comment(5)
Please post the code!!!Pavlov
@shekharsuman Actually it's clear, that's a problem with compiler settings in IDE.Dahlgren
I doubt that unless clarified by OP! @MarounMaroun...Pavlov
I just did a test in IntelliJ. If I have the project language level set to 6, I see the problem you have. If I change it to 7, the "problem" goes away.Employee
@shekharsuman why do you doubt that? Its most probaly the reason. he installed java 1,7, but the project has setted compiler version to 1.6 or priorGoodill
G
17

You need to change language level in your IDE.

Check these settings:

  • File > Project Structure > Project > Project SDK
  • File > Project Structure > Project > Project Language Level
  • File > Project Structure > Modules > Your module > Sources > Language Level
  • File > Project Structure > Modules > Your module > Dependencies > Module SDK

Also check compiler settings. Sometimes it adds extra arguments to compiler:

  • File > Settings > Compiler > Java Compiler > Byte code version

If you use maven plugin enable auto-import. Then the language level will be detected automatically from your pom.xml settings.

Gaylordgaylussac answered 10/9, 2014 at 15:13 Comment(1)
This is really helped me in intellij 2021.2.3Uncalledfor
G
1

In your project settings most probably you use java compiler java 1.6 or prior. Change that to java 1.7

Goodill answered 10/9, 2014 at 15:12 Comment(0)
G
0

I think you have installed multiple JDKs on your system or you are using the default JDK that is bundled with IDE intellij. Try this link to change your JDK path to the one that is 1.7 or higher if any installed on your system or update your JDK to the latest version available

Gavan answered 10/9, 2014 at 15:17 Comment(0)
A
0

I solved my case like this:

Intellij

File -> Project Structure -> Project -> Project language level:

And in the options box I selected the option:

14 - Switch expressions

Aright answered 12/9, 2022 at 13:54 Comment(0)
I
0

If you add this to your pom.xml file it will resolve the issue where the key part is the source 1.8:

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>      
Insect answered 19/12, 2022 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.