Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum question

 
Tomita Militaru
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I get in Eclipse the error: Syntax error on token "-" , , expected

I'm trying to use switch with some strings, thats why I use Enum. How can I compare the string "font-weight" if I can't declare it in the enum class?

Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use switch with some strings, thats why I use Enum. How can I compare the string "font-weight" if I can't declare it in the enum class?


Enums are not strings. How are you considering using enums to compare strings?

Henry
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enum member names have to be valid Java identifiers; you can't use the "dash" character.

You can always just use "if (s.equals("font-weight"))... else if (s.equals("rowspan"))..." . Or if you have a large number of keywords, use a Map with the keywords as the keys, and "action objects" as the values -- i.e., objects that all implement a simple interface like Runnable to perform the action you want to perform.
 
Tomita Militaru
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
I'm trying to use switch with some strings, thats why I use Enum. How can I compare the string "font-weight" if I can't declare it in the enum class?


Enums are not strings. How are you considering using enums to compare strings?

Henry


I'm trying to use this : http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

You can always just use "if (s.equals("font-weight"))... else if (s.equals("rowspan"))..." . Or if you have a large number of keywords, use a Map with the keywords as the keys, and "action objects" as the values -- i.e., objects that all implement a simple interface like Runnable to perform the action you want to perform.


Yes, I have a large number of keywords and for each I need to call a method. I have a map with keyword & value . Could you please explain in more detail how can I do that?
Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomita Militaru wrote:
Henry Wong wrote:
I'm trying to use switch with some strings, thats why I use Enum. How can I compare the string "font-weight" if I can't declare it in the enum class?


Enums are not strings. How are you considering using enums to compare strings?

Henry


I'm trying to use this : http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html



Yes, but where is that tutorial does it show that enums and strings are interchangable?


BTW, this question was rhetorical, I am not trying to give you a hint towards this...

Henry
 
Tomita Militaru
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well ...


... and



Thats what I was trying to do.

 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Interesting. My apologies -- I didn't know that Java provided a valueOf() method to convert a string to an enum.

Anyway, as an alternative, I guess you can always use font_weight as the enum value, and then, slightly change the toDay() method like so...



Henry
 
Tomita Militaru
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, you really helped me alot!
Have a good day!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic