File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Enum question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum question" Watch "Enum question" New topic
Author

Enum question

Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37


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.


Poor is the man whose pleasures depend on the permission of another.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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

Joined: Jan 16, 2009
Posts: 37
Well ...


... and



Thats what I was trying to do.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


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

Joined: Jan 16, 2009
Posts: 37
Thank you guys, you really helped me alot!
Have a good day!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum question