Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot switch on a value of type String for source level below .." error in Eclipse

Tags:

java

eclipse

I am using Eclipse to develop a Java program. I had to downgrade JRE and JDK from 1.7x to 1.6. Now everything is pointing to 1.6.x (including the installed JRE and JDK compliance).

But now Eclipse still gives me an error on the switch statement, indicating:

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted

on the code below:

Switch("test") // Which is fine with 1.7.x

I removed 1.7.x from computer, not sure why it is still looking for 1.7 instead of 1.6?

like image 217
Tony Avatar asked Aug 01 '12 19:08

Tony


4 Answers

Switching on strings was introduced in Java 1.7!

The error message is expected when you downgrade to Java 1.6. In that version you can only switch on primitive types and enums.

Related question:

  • Why can't I switch on a String?
like image 151
aioobe Avatar answered Oct 10 '22 11:10

aioobe


Right click on your project, go to Properties. Select Java Compiler from the left menu. Select your compliance level (1.7 or 1.6). 1.7 will stop that message. 1.6, as others said before, won't let you use strings.

like image 35
live-love Avatar answered Oct 10 '22 11:10

live-love


Actually, your code is not valid on 1.6. You can't do a switch on a String.

like image 24
Vitor De Mario Avatar answered Oct 10 '22 11:10

Vitor De Mario


switch(String) is syntax applicable from Java 7 onwards. Because you have 1.6 which doesn't support switch(String), eclipse giving compilation error.

Change switch(String) to switch(int)

like image 29
kosa Avatar answered Oct 10 '22 11:10

kosa