• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to Enum

 
Sachin Ramesh Vir
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above program, I am using enum for the operating system. The switch case needs to be performed based upon the user input. The return type of readLine() is string. How can we change the String to enum.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ignore this post as I have no idea what I'm talking about!

Many problems here;

1.) you need to read about enumeration as you're not using it properly. It cycles though a collection one element at a time.

this statement



makes no sense(unless it's something in 5.0 that I don't know) it should be something like this (psuedo code off the top of my head)

CollectionType collection = {windows, unix, linux, macintosh};
Enumeration e = collection.elements();
e.nextElement;

2.) This statement makes no sense


You'd need a class named OperatingSystems to make any sense of this at all. You probable meant to use a String here.

3.) switch statements use char, or something that can be evaluated to a char. Strings won't work.



you're trying to evaluate to a string here, well actually an OperatingSystems according to your code but that makes even less sense.


Start smaller, get a switch statement to compile and run and then add the next piece.

[ September 20, 2005: Message edited by: Hentay Duke ]
[ September 20, 2005: Message edited by: Hentay Duke ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enum is a new keyword in 5.0.
Nothing to do with the Enumeration class.
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use this :


the catch IllegalArgumentException means the user provided an invalid entry.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hentay Duke:
Many problems here;

1.) you need to read about enumeration as you're not using it properly. It cycles though a collection one element at a time.

this statement



makes no sense(unless it's something in 5.0 that I don't know) it should be something like this (psuedo code off the top of my head)


Actually, it makes complete sense in Java 5.0. It is something you don't know yet, apparently

Layne
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an excellent question, Sachin, and it's one that will be raised frequently going forward.

Individual enums tend to be short (dozens not hundreds) and the enum has a set of very useful helper methods. The name attribute string-izes the value of the enuemrations, for example.

for( Color color : Color.values() )
{
if( color.name().equals("Blue") ) ... do something ...
}

If you use it a great deal it would make sense to create a map of string values and enum values. Declare a Map<String, Color> and populate as follows:

for( Color color : Color.values() )
{
map.put(color.name(), color);
}
[ September 20, 2005: Message edited by: Rick O'Shay ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Rick]: If you use it a great deal it would make sense to create a map of string values and enum values.

For what? Seems to me that we get everything we need from the valueOf(), name(), and values() methods already built into enums:

I'm not sure what else we'd need...
[ September 20, 2005: Message edited by: Jim Yingst ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So sorry, maybe I'll just be quiet until I get up to speed with 5.0.

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a mapping from the enum field name to the enum instance itself implicitly using the valueOf method. Quite often, you need to use something other than the field name as the key, in which case, the API Specification recommends the use of EnumMap. Being the person I am, I recognise that this approach is somewhat flawed, so I suggest using a Map, perhaps lazily loaded (within a static nested class of the enum type). At least, this is what I do.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is true, you do have everything you need with the possible exception, npi, that Color.valueOf("seafoam") would throw an IllegalArgumentException should somebody have the audacity to create a Color enumeration without it. With a map you don't get shot just for asking. Then again you have to pack that map with you so the built-in is far better on balance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic