0

I have re-installed my Java 8 and get a new and fresh Eclipse version.

If I write the following code:

String a = "001100";

switch(a) {
   case "5": a = "25"; break;
   default: System.exit(0);
}

then Eclipse underlines the "a" in the switch-brackets red and says "Change project compliance and JRE to 1.7". I have not tried this because I'm using Java 1.8 and this should work? I don't want to use Java 1.7.

My Eclipse is setting up with Java 1.8, also this Project. There is no lower Java version in the settings....

Where is the problem?

enter image description here

Kara
  • 6,115
  • 16
  • 50
  • 57
krokvskrok
  • 197
  • 2
  • 10
  • switch case constructs are constants and string values are not constants. Also you seem to have the project source levels set to 1.7 instead of 1.8. Try changing it in the project properties- – Nazgul May 01 '14 at 11:01

5 Answers5

1

according to this answer switching for strings is implemented since JDK7 so switching to 1.7 compliance should do it. As I understand this is not 'switching back' your java version instead it enables you to use those 1.7 features.

Community
  • 1
  • 1
xmoex
  • 2,602
  • 22
  • 36
1

The problem most probably is that your language level is set below java 1.7. It is not the same as JRE version used by project. try to find language level in your project settings and set it to Java or Java 7 for this switch on String to be working

Belerafon
  • 488
  • 4
  • 13
1

Your Eclipse is using a Java SDK version lower than 1.7. Check your Java compiler level in Eclipse:

Windows>Preferences>Java>Compiler>compiler compliance level

Andrew Lobley
  • 342
  • 4
  • 12
  • If i set the compliance level for the specific projekt to 1.7 then it works. But if i set the compliance level to 1.7 generally in eclipse like in your answer and then create a new projekt, then this new project has again a compliance level of 1.4.... – krokvskrok May 01 '14 at 11:14
  • If you right click a project, and then go to properties, you are changing these only for the project. If you follow like what I said, it will be general preference in general. If you look at top-left corner of the preference window (window in which compliance level is adjusted), the title should be preference. Also, while creating the project, make sure you are selecting the correct JRE in "Use an execution environment JRE". – Andrew Lobley May 01 '14 at 11:28
0

You must set the default JDK version for the eclipse, to take all the projects to be created in default JDK version eclipse.

to know how to set the default JDK version in eclipse click here

Community
  • 1
  • 1
Selva
  • 546
  • 5
  • 12
  • 34
0

Strings in switch statements are available only post JDK 1.7

https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

You need to check your java compiler level and change it to JDK 1.8 or 1.7.

(Right Click on project, go to preferences then look for "Java Compiler" and change "Compiler Compliance Level")

Gabriel Panza
  • 309
  • 2
  • 10