开发者

String in switch java

switch(title)
   {
    case "Return":
          returnBooks();
          break;
  }

strin开发者_开发技巧gs in switch are not supported in -source 1.6, use -source 7 or higher to enable strings in switch

So, I have already installed JDK7, Why compiler show this message and giving an error while on compiling?

I am using NetBeans


I hate to say it, but: read the darn error message. It tells you exactly what the problem is.

You are compiling in Java 6 mode, not compiling in Java 7 mode. If you're using javac that comes with JDK 7, as the error message says, use the -source 7 flag:

> javac -source 7 {stuff here}


Two key points from the user query:

  1. Already installed JDK7;and
  2. Using Netbeans

So, netbeans came with two package:

  1. With JDK

Means when install the Netbeans 7 it automatically install JDK 7 and set JDK 7 as Default Library.

  1. Without JDK

On the installation of NetBeans 7, it found JDK in the system, and if found it automatically configure the path of library as well, but show you information about the path and version of JDK7.

All of users try to give the correct answer of this query, but no one is in position to answer in the correct way as the user required because user using the Netbeans as well.

With continue the edit version of Hemal Pandya, one thing more is required to configure, which is that

RightClick on  Project > properties > and in the categories option select > source. see the  
**Hemal Pandya** edit version to look at image, the source option is available above the   
Libraries option.

And

then select **Source/Binary Format** form bottom and set it to JDK 7 (= 1.7). this is the exact solution
of user's post and I am 100% sure now String in swich will work


In NetBeans, go to the properties of the project and change the java platform to java 7 in libraries


I have found a solution: If you are using Netbeans 7.1 (even, 7.0), just you have to follow this: Right Click on Project -> Properties -> Sources -> (On the bottom) Source/Binary Format

Just select JDK 7, and that's all, you can then use strings in switch!


I do not use NetBeans but there seems to be a compliance switch that has defaulted to 1.6. You will have to find that switch and set it to 7, as others have pointed out.

EDIT: I found I found netbeans.org/kb/docs/java/project-setup.html#projects-jdk. You seem to have done the second step of registering jdk. But maybe it is not the default? Follow the instructions to To switch the target JDK of a standard project. Looking at images it seems to be in this dialog:

String in switch java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜