• Post Reply Bookmark Topic Watch Topic
  • New Topic

Words like "abstract" in enums.  RSS feed

 
Kevin Vaughn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a dictionary list in an enum and the problem is that some of the words like char, class, abstract, implements, etc. are being interpreted as commands instead of simply being strings/elements in the enum. Is there some way to override this?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin,

Welcome to JavaRanch!

No, you can't use Java reserved words as enums. You'll have to change the spelling or the case. So "class" doesn't work, but "clazz" or "Class" would.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java reserved words and identifiers are case-sensitive. Have you tried using All Caps for the identifiers? That's a common convention for enum names and static final values.

This should compile:

On the other hand, isn't that going to be an awfully long list of enum values? How exactly are you intending to use these enum values? Maybe there's some other way you can achieve your goal? Just wondering.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to have the unadulterated version of the name somewhere, you can have an additional String field in the enum, that stores the name. E.g.:

Or if the labels are ALWAYS just lowercase versions of the identifier name, you can do something simpler:
 
Kevin Vaughn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Java reserved words and identifiers are case-sensitive. Have you tried using All Caps for the identifiers? That's a common convention for enum names and static final values.

This should compile:

On the other hand, isn't that going to be an awfully long list of enum values? How exactly are you intending to use these enum values? Maybe there's some other way you can achieve your goal? Just wondering.


Its going to be a dictionary, so I'll have like 175,000 words going into the enum. I figured this would be the easiest way to implement it since they are already in a list format (I can just copy and paste it from Word). I'm making a word game and this is like the dictionary to check if a word is legit as you would in a game of Scrabble. So I was planning on having the word string that was played cycled through the dictionary terms using a for loop to check if it matches. If there is an easier way, I'm open to suggestions, but this is the best that I can think of.

This is how I would use it (currentWord is the word being played, disregard the middle content, the rules are different than Scrabble):

Thanks guys. I just converted all of the words to uppercase and converted the word played to uppercase so that they will match. Problem solved.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Vaughn wrote:
Its going to be a dictionary, so I'll have like 175,000 words going into the enum. I figured this would be the easiest way to implement it since they are already in a list format (I can just copy and paste it from Word).


Yikes ! Copy/paste from Word may work but an enum is definitely NOT a good way to do this. 175K values is definitely too many for an enum. When you add words to your dictionary, you're going to need to add an enum and recompile your code. A dictionary should be implemented as a repository of some sort: text file, database table, etc. that you can update it independently from the code. The code is the logic and should be kept separate from the data it uses.

Also, the way you're searching for a match is about as inefficient as you can get: it's a linear search but that's almost beside the point. First thing that comes to my mind is to use an in-memory database that you can initialize/persist from/to a text file. Enum would definitely NOT be an option for me. Check out http://db.apache.org/derby/docs/10.7/devguide/cdevdvlpinmemdb.html or HSQLDB
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!