• 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

retrieve a variable that matches a string?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an array of people's names.
I also have variables with the same names as the strings in the array I mention above.
Is there anyway to take the name strings I retrieve from the array and retrieve the variable of the same name?
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there might be a way, using reflection. but i'm not very practiced at that API, so i'll just point you at sun's tutorial on the subject.

however, i can't help but think you probably really want to be using a Map of some sort instead of the data types you actually are using. i think that would accomplish the same end more easily. look them up in the API, and read the collections tutorial if you haven't already.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying that you want the variable's value or the variable's label to be the same as the data in the array element? If you want the value, you could construct one of the Java collections, add each array element to the collection either directly or in the constructor, then use one of the collection methods to check for existence of the record. If you want to rename the variable, I don't think you can do that...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of "variable" are we talking about here? A field of an object?

Can you show us the code you have?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic