2

I created an enum like so

public enum Direction {
    NORTH, SOUTH, WEST, EAST, NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
}

then I try to use it in a switch statement

 Direction direction = Direction.NORTH;
    switch(direction){
    NORTH:
        System.out.println("Syntax error on token {, case expected after this token");
        break;
    }

I am getting the error I put in the println...

CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • 8
    case NORTH: ..... – Eran Apr 02 '15 at 05:52
  • 1
    3 upvotes for zero research effort? Ok... – Tom Apr 02 '15 at 06:22
  • @Tom 1 downvote for envy effort? Ok... Rule #1 if you have a problem even if minor and you think it is a silly problem the truth is many people will have that same problem. – CodeCamper Apr 02 '15 at 13:06
  • @CodeCamper And? Even if they have same problem, they _could_ be able to spent 5 seconds to research and they are done with it. – Tom Apr 02 '15 at 14:39
  • @Tom I spent 10 seconds but I couldn't see the problem because the all capitals. I had other enum code with case but for something reason my eyes tricked me with the capital letters. Also I learned something new because of the answer talking about Enum Methods to avoid this all together. – CodeCamper Apr 02 '15 at 14:50

2 Answers2

5

You miss the case keyword.

switch(direction){
case NORTH:
    System.out.println("Syntax error on token {, case expected after this token");
    break;
}

Demo

singhakash
  • 7,891
  • 6
  • 31
  • 65
riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
3

While not directly answering the question, I would suggest adding a method to the enum (Java Enum Methods) and calling the method instead. This would make it cleaner if and when we add a new enum type. We don't have to make modifications to the switch case, just add the implementation for the newly added enum type.

Community
  • 1
  • 1