Jpeppa
Jpeppa

Reputation: 71

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

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

Upvotes: 6

Views: 5015

Answers (5)

user8128167
user8128167

Reputation: 7676

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>      

Upvotes: 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

Upvotes: 0

ponomandr
ponomandr

Reputation: 1534

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.

Upvotes: 17

Hanzallah Afgan
Hanzallah Afgan

Reputation: 734

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

Upvotes: 0

AlexWien
AlexWien

Reputation: 28737

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

Upvotes: 1

Related Questions