• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to Enum

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
enum is a new keyword in 5.0.
Nothing to do with the Enumeration class.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use this :


the catch IllegalArgumentException means the user provided an invalid entry.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So sorry, maybe I'll just be quiet until I get up to speed with 5.0.

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic