3

Can someone please tell me why the switch statement is not recognizing the gat String variable. The IDE tells me that a primitive is required (int, char, short ....), but it found a string.

String gat = temp[i];

switch (gat) {
    case "a":
        output[i] = 12 * k;
        break;
    case "b":
        output[i] = 23 * k;
        break;
    case "c":
        output[i] = 34 * k;
        break;
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
matt lao
  • 331
  • 1
  • 2
  • 11

2 Answers2

4

Your project compliance level is set to Java 6 or earlier, you cannot use String as case labels before Java 7. But, in the case of your question you might use charAt(0)

String gat=temp[i];
switch (gat.charAt(0))
{
case 'a':
    output[i] = 12 * k;
    break;
case 'b':
    output[i] = 23 * k;
    break;
case 'c':
    output[i] = 34 * k;
    break;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

If you are using NetBeans, make sure you have the latest JDK version. If you are using Eclipse, have the latest JDK version and set compliance level of the compiler in Java settings to 1.7.

shryesh.k
  • 51
  • 1
  • 7