9

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;
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
user1514362
  • 127
  • 1
  • 2
  • 3

6 Answers6

11
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
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
kosa
  • 65,990
  • 13
  • 130
  • 167
6

If you are using maven project then simply you can add following piece of code to ypur pom.xml and resolve your problem :

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
Suraj Jogdand
  • 308
  • 2
  • 17
  • 1
    Looks like Maven stuck in medieval times. I had never ever thought that such obvious thing like standard java API should be enabled explicitly by adding some lines in maven pom file. It works tho, thanks! – Kirill Karmazin Dec 28 '18 at 17:06
4

switch accepts a String from java 7. prior to java 7 only int compatible types (short,byte,int, char) can be passed as switch arguments

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • I'm really sorry about that. I have been trying to remove this downvote and it tells me I'm not allowed to change my vote. It was an accidental downvote, is there an undo? – krico May 16 '13 at 14:27
  • @krico i have just edited my post, you can now revert your downvote .. :) – PermGenError May 16 '13 at 14:30
1

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>
b.s
  • 2,409
  • 2
  • 16
  • 26
zjf
  • 11
  • 1
0

You cannot switch over a String instance, only int (and byte/char/short, but not long/double), unless you have Java7+. Your best option now is to change to if else statements, like so:

if("B".equals(string)) {
    //handle string being "B"
} else if("C".equals(string)) {
    //handle string being "C"
} else ...

For more info on switching, see the Oracle tutorial. They mention the Java7 String functionality:

In Java SE 7 and later, you can use a String object in the switch statement's expression.

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • @user1514362 What do you mean? I gave you an example which doesn't use a switch statement but perfoms the same thing. Isn't this what you want? You must remove your switch statement to make it work – Alex Coleman Sep 20 '12 at 21:49
-1

In Java, you can only do a switch on byte, char, short, or int, and not a String.

Don Branson
  • 13,631
  • 10
  • 59
  • 101