20

I want to put the special characters, the parentheses ( '(' and ')' ) and the apostrophe ('), in an enum.

I had this:

private enum specialChars{
   "(", ")", "'"
}

but it doesn't work. Java says something about invalid tokens. How can I solve this?

Grtz me.eatCookie();

Confituur
  • 495
  • 1
  • 6
  • 16

3 Answers3

25

You could do something like this:

private enum SpecialChars{
   COMMA(","),
   APOSTROPHE("'"),
   OPEN_PAREN("("),
   CLOSE_PAREN(")");

   private String value;
   private SpecialChars(String value)
   {
      this.value = value;
   }

   public String toString()
   {
      return this.value; //will return , or ' instead of COMMA or APOSTROPHE
   }
}

Example use:

public static void main(String[] args)
{
   String line = //..read a line from STDIN

   //check for special characters 
   if(line.equals(SpecialChars.COMMA)      
      || line.equals(SpecialChars.APOSTROPHE)
      || line.equals(SpecialChars.OPEN_PAREN) 
      || line.equals(SpecialChars.CLOSE_PAREN)
   ) {
        //do something for the special chars
   }
}
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • This does not work for me: my compiler complains: `Type mismatch: cannot convert from EnumTest.SpecialChars to String` – Edward Mar 30 '16 at 11:18
  • @Edward Did you override `toString()` as in the example above? – Hunter McMillen Mar 30 '16 at 12:57
  • @HunterMcMillen: Yes, but using `if(line == SpecialChars.COMMA || ...` did not work. Actually, when calling the toString Method, it works (`if(line == SpecialChars.COMMA.toString() || ...`) So, should this also work without calling `toString()`? – Edward Mar 30 '16 at 13:03
  • Sorry, looks like you found a 3-year old mistake :) In Java, you need to use `.equals()` to compare String objects. So the correct form would be `if ( line.equals(SpecialChars.COMMA) .... ) { ... }` – Hunter McMillen Mar 30 '16 at 14:37
5

Enum constants must be valid Java identifiers. You can override toString if you would like them displayed differently.

public enum SpecialChars {

    OPEN_PAREN {
        public String toString() {
            return "(";
        }
    },

    CLOSE_PAREN {
        public String toString() {
            return ")";
        }
    },

    QUOTE {
        public String toString() {
            return "'";
        }
    }

}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 4
    @EugeneKuleshov You are _supposed_ to override toString() for every class you create. – Hunter McMillen May 01 '12 at 14:40
  • @EugeneKuleshov Oracle thinks otherwise: ["An enum type should override this method when a more "programmer-friendly" string form exists."](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html#toString%28%29) – Sergey Kalinichenko May 01 '12 at 14:40
  • 1
    @EugeneKuleshov What's actually not a good idea is downvoting competing answers :) – Sergey Kalinichenko May 01 '12 at 14:42
  • It has nothing to do with competition. There are bunch of specical characters that are just not printable (e.g. \n, \t, \r), so I stand my ground in this case. In this particular case, the parent toString() is actually more programmer-friendly. – Eugene Kuleshov May 01 '12 at 15:10
  • @EugeneKuleshov The OP did not mention non-printable characters anywhere in his post, so the assumption that he has any plans to use them is questionable, to say the least. – Sergey Kalinichenko May 01 '12 at 15:16
  • @EugeneKuleshov the parent toString() for an enum will only print the name of the enum itself. With the parent toString() the OP would see `COMMA` instead of `,` which is not what they want. – Hunter McMillen May 01 '12 at 15:55
  • @HunterMcMillen the OP didn't say what he want. Like I said, the "," won't really represent given enum and if you need an actual char, there should be a separate method for getting it. Oh, and don't even get me started on returning a String value for a single character... – Eugene Kuleshov May 01 '12 at 17:07
  • @EugeneKuleshov When you print an enum its toString is called implicitly. I don't see any waste in returning my custom string when Java is returning its own string anyway. – Hunter McMillen May 01 '12 at 20:01
2

You should use something like this instead:

private enum SpecialChars {
   LEFT_BRACKET('('),
   RIGHT_BRACKET(')'),
   QUOTE('\'');

   char c;

   SpecialChars(char c) {
     this.c = c;
   }

   public char getChar() {
     return c;
   }
}
Michael Hogenson
  • 1,292
  • 1
  • 15
  • 31
Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67