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

Here is an example:

String typeOfDay;
         switch (dayOfWeekArg) {
             case "Monday":
                 typeOfDay = "Start of work week";
                 break;
             case "Tuesday":
             case "Wednesday":
             case "Thursday":
                 typeOfDay = "Midweek";
                 break;
             case "Friday":
                 typeOfDay = "End of work week";
                 break;
             case "Saturday":
             case "Sunday":
                 typeOfDay = "Weekend";
                 break;
             default:
                 throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
         }
2
  • Please format your code using the "code" tool of the editor! Commented Sep 18, 2015 at 15:19
  • 1
    How do you compile your code? using plain javac? Or IDE (which one) ? Commented Sep 18, 2015 at 15:41

2 Answers 2

20
Cannot switch on a value of type String for source level below 1.7

You aren't using jdk 8. You either need to update java or fix the compiler compliance level of your IDE.

To change compiler compliance level in Eclipse:

Open Window > Preferences > Java > Compiler

Change the Compiler compliance level under "JDK Compliance" to 1.8


To change the source level in Netbeans:

Right-click the Libraries node in the Project view and choose Properties.

Choose "Sources" and set the Source Level to 1.8


To change the project bytecode version in IntelliJ IDEA:

Open File > Settings > Build, Execution, Deployment > Compiler > Java Compiler

Set the Project bytecode version to 1.8

Sign up to request clarification or add additional context in comments.

Comments

1

It's highly recommend you use if ("Monday".equals(dayOfWeekArg)) instead of switch. Because it support all JDKs. And nowadays, most of applications are running on JDK 1.7.

1 Comment

" And nowadays, most of applications are running on JDK 1.7." which is quite worrying since 1.7 reached its end of life for the general public (java.com/en/download/faq/java_7.xml)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.