Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Statement gives Incompatible Types error

I am trying to compile and I get this error:

enigma/Rotor.java:30: incompatible types found : java.lang.String required: int     switch(name){
1 error

Why am I getting this error? How do I fix it? It's in the package and I can't seem to figure it out. Here's the code:

String label;

Rotor(){;}

Rotor(String name){
  switch(name){
    case "B":
      conversion_chart = B;
      break;
    case "C":
      conversion_chart = C;
      break;
    case "I":
      conversion_chart=I;
      notch = NOTCH[0];
      break;
    case "II":
      conversion_chart=II;
      notch = NOTCH[1];
      break;
    case "III":
      conversion_chart=III;
      notch = NOTCH[2];
      break;
    case "IV":
      conversion_chart=IV;
      notch = NOTCH[3];
      break;
    case "V":
      conversion_chart=V;
      notch = NOTCH[4];
      break;
    case "VI":
      conversion_chart=VI;
      notch = NOTCH[5];
      break;
    case "VII":
      notch = NOTCH[6];
      conversion_chart=VII;
      break;
    case "VIII":
      notch = NOTCH[7];
      conversion_chart=VIII;
      break;
  }
  label = name;
  position = 0;
}
like image 523
user1514362 Avatar asked Sep 20 '12 21:09

user1514362


People also ask

What is incompatible type error?

The incompatible types error most often occurs when manual or explicit conversion between types is required, but it can also happen by accident when using an incorrect API, usually involving the use of an incorrect reference type or the invocation of an incorrect method with an identical or similar name.

What data types are supported in switch statements?

A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character , Byte , Short , and Integer (discussed in Numbers and Strings).

Which datatype is not allowed in switch in Java?

It doesn't allow variables. The case values must be unique. In case of duplicate value, it renders compile-time error. The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.

Does switch statement execute all cases?

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using the strict comparison, === ) and transfers control to that clause, executing all statements following that clause.


2 Answers

switch(name)

switch statement with String is supported from Java7 onwards only.

I guess the compiler version you are using is less than Java7

Options:

  1. You need to either upgrade to Java7
  2. Change switch statement to if/else
  3. Use int in switch instead of String
like image 160
kosa Avatar answered Sep 23 '22 02:09

kosa


If you're using maven then change build in pom as following else change JDK version as 1.8+

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
like image 31
zjf Avatar answered Sep 27 '22 02:09

zjf