• 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

converting a string color to an object color

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Would any one please shed a light.
how to convert a String like:

String colorStr = "Red";
to an Object Color.red

Thanks.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this what you were looking for?
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Map would be better than two separate arrays, then you could have a getColour method which took a String as a parameter and use that as a key into the Map. The user wouldn't have to know the index of each of the colours then.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
joanne, i would marry you if i could
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:joanne, i would marry you if i could


I think Campbell is first in line at the moment, but feel free to try and push in front.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote: . . . I think Campbell is first in line at the moment, but feel free to try and push in front.

Oh, Joanne, how sweet of you.

As long as Ruth doesn’t find out. And as long as Eleanor (my smaller daughter) doesn’t think I am marrying somebody younger than herself
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(blushing) Bringing this thread back on-topic... the other part of the answer to the original question is "No, you may find it surprising, but there is nothing in the standard Java API to convert strings to Color objects, at least, not in the way you describe."
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much that is exactly what I was looking for.

Randall Twede wrote:is this what you were looking for?

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to map the named constants of the Color class (or any other) which are instances of the same class, here's a utility I wrote some time back that uses Reflection to do it.Usage to obtain a Map of static Color fields of the Color class:
edit: Thought I should mention that the class is abstract to force instances to be named or anonymous subclasses, which makes it possible to obtain the ParameterizedType from the genericSuperclass.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class is missing the following three methods:
The first two are required as per the contract of java.util.Map.

I actually don't like this trick. Instead of an entire class I'd use one static utility method instead:
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Your class is missing the following three methods:

The first two are required as per the contract of java.util.Map.


Thanks Rob, for pointing that out. I'll keep that in mind if I ever publish the class.

Rob Spoor wrote:I actually don't like this trick. Instead of an entire class I'd use one static utility method instead:


I actually had this approach too, and the code is very similar to yours (but yours is more complete in that it checks the modifiers)I didn't think of using Class#cast(...) instead of suppressing the warning.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic