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;
}
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.
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).
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.
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.
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:
if/else
int
in switch instead of String
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With