-4

I am trying to build a simple program that demonstrate the use of the String class being evaluated in a switch statement but i'm getting a compilation error because this feature requires Java SE 7 (whish is installed in my computer).

Error message on line 5: Cannot switch on a value of type String. Only convertible int values or enum cons tants are permitted

Is Eclipse pointing at JRE7 ? = YES . Is Environnement variable Pointing at JDK7 =YES .

java -version = OK (see pict below). enter image description here

I even tried to compile the code with the console prompt. See error: enter image description here

import javax.swing.JOptionPane;

public class SwitchDemo {
    public static void main(String[] args) {

        String name = "georges";

        switch (name.toLowerCase()) {


        case "Jhon":
            JOptionPane.showMessageDialog(null, "Good morning, Jhon!");
         break;
      case "georges":
             JOptionPane.showMessageDialog(null, "How's it going, georges?");
         break;
      case "sergei":
             JOptionPane.showMessageDialog(null, "sergei, my old sergei!");
          break;
      case "Steph":
             JOptionPane.showMessageDialog(null, "Afternoon lennert, how's the Steph?");
          break;
      default:
             JOptionPane.showMessageDialog(null, "Pleased to meet you, xxxxxx.");
           break;
   }

    }
}

enter image description here

AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • 5
    The name of your main class is `SwitchDemo`, not `switchDemo` – fge May 28 '13 at 10:39
  • Absolutely. It works now but only when i compile with cmd. But Eclipse keeps showing a compile error though. Perhaps i should restart my computer – AchillesVan May 28 '13 at 10:46

1 Answers1

8

From your command line I see that you run java -cp .switchDemo, while your actual class name is SwitchDemo. Notice that Java is case sensitive, thus SwitchDemo != switchDemo.

From your comments:

Eclipse keeps returning the error message. I'm currently on Eclipse Helios. I'll switch over to Juno. Thank you

you have to change your compiler to 1.7 in eclipse:

enter image description here

PermGenError
  • 45,977
  • 8
  • 87
  • 106