0

I know this not going to compile and run at this point. However is the switch statement right? I did get a compiler error of: class, interface, or enum expected

public class Fruit
{

  public static void main(String[] args)
  {

String choice = " ";

    switch(choice)
    {
    case " A ":  System.out.print(" Apple");
      break;

    case  " K ": System.out.println("Kiwi");
      break;

    case " P ": System.out.println("Pear");
      break;

    default: System.out.println("incorrect choice");
    }
  }
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
jjb
  • 11
  • You can not switch over string apparently Here is a big discussion on that http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string – maaz Sep 01 '14 at 01:07
  • Your code syntax is correct. You might want to check the version of Java that you're using; try updating the Java development kit. – daOnlyBG Sep 01 '14 at 01:10
  • My version is JDK 7.0_25 – jjb Sep 01 '14 at 04:06

3 Answers3

5

You are getting the error because you must be trying to use switch with String with Java version lower than 7. String support for switch statement was introduced in Java 7 and hence you need to be on the same version or higher to compile your code.

Follow this tech note to learn more about switch with String:

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

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 4
    In my experience, it is semi rare to need to switch on a string. It might be prudent to rethink why you are trying to switch on a string and to consider using something like an Enum instead. – Cogman Sep 01 '14 at 01:13
  • I agree. Don't switch on a string. In this case it looks like you should be switching on a char, which has always been allowed. – Paul Boddington Sep 01 '14 at 01:59
0

Strings in switch Statements

In the JDK 7 release, you can use a String object in the expression of a switch statement.

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

As mentioned, you can use a string as an expression of a case statement only with Java 7 or higher. If you can use only lower versions, then here is a work around for your code.

  1. An alphabet has a number representation. That is, CHARACTERS have fixed number codes for them - http://www.kerryr.net/pioneers/ascii2.htm

  2. You will get a String, get the first char in the string and then convert that char to its int code.

  3. Use int code instead of String choice in the old fashioned switch.

    public class Fruit {
    
    public static void main(String[] args) {
    
        String choice = "";
        int code = -1;
    
        choice = "K";
        code = (int) choice.charAt(0);
    
        switch (code) {
        case 65://A
            System.out.print("Apple");
            break;
    
        case 75://K
            System.out.println("Kiwi");
            break;
    
        case 80://P
            System.out.println("Pear");
            break;
    
        default:
            System.out.println("incorrect choice");
        }
      }
    }
    
Erran Morad
  • 4,563
  • 10
  • 43
  • 72