Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Switch statement using a String with Java 1.7

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch statement using a String with Java 1.7

    Hi,

    switch(name) //Where name is a String
    {
    Code block
    }

    I am trying to use a String switch statement like the one just above. I am getting the following error:

    Error: Cannot switch on a value of type java.lang.String. Only convertible int values or enum constants are permitted.

    After some research, a String can only be used in a switch statement in Java 1.7. I do have the most up to date version of Java 1.7 (checked on the java website) and I am still getting that error. Does anyone know why and how I can fix it?

    Additional info: Compiler is Eclipse Compiler 0A48 with Dr. Java.

    Thank you.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Switch statement using a String with Java 1.7

    From the wording of the error message it seems eclipse believes you wish to use an older version of java for that project.
    I have encountered this error before and that was the cause of the issue for me. There is a way to tell eclipse to use a specific version by default, and a way to set the version per project. Unfortunately I do not remember how to make the change. Not much of an eclipse guy yet..

    Perhaps some poking around on your own or with a search engine will help you determine if that is your problem or not.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Switch statement using a String with Java 1.7

    Right-click on your project and Open the project properties.

    Then on the left menu, select Java Compiler. This will list the project-specific JDK compliance version that project is using.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    jps (November 24th, 2012)

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  3. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2010, 11:31 PM
  4. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  5. How to Use the Java switch statement
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: October 8th, 2009, 09:00 PM