It works for me in NetBeans too, so if it does not even start on your machine, you may have some verification tool, linter, something like that.
Anyway, what you write is correct Java code, just probably it will not do what you expect, and that is what the message may indicate.
You can switch on numbers, characters and Strings, all of them work. Just do not mix them: they either will not compile (mixing numbers/characters with String), or will work in a probably unexpected way (mixing numbers with characters), because 1 is a number but '1' is a character, which is represented by its ASCII code as a number, 49.
Test code:
String x="1";
int z=Integer.parseInt(x);
switch(z){
case ('1'): System.out.println(z+" is '1'");break;
default: System.out.println(z+" is not '1'");
}
x="49";
z=Integer.parseInt(x);
switch(z){
case ('1'): System.out.println(z+" is '1'");break;
default: System.out.println(z+" is not '1'");
}
x="1";
z=Integer.parseInt(x);
switch(z){
case 1: System.out.println(z+" is 1");break;
default: System.out.println(z+" is not 1");
}
switch(x.charAt(0)){
case '1': System.out.println("\"1\".charAt(0) is '1'");break;
default: System.out.println("\"1\".charAt(0) is not '1'");
}
switch(x){
case "1": System.out.println("\""+x+"\" is \"1\"");break;
default: System.out.println("\""+x+"\" is not \"1\"");
}
Output:
1 is not '1'
49 is '1'
1 is 1
"1".charAt(0) is '1'
"1" is "1"
z1or49(the ASCII value of character 1)? Also as a stylistic thing, the parentheses in the case labels look weird and may confuse people.case 1:andcase 2: