Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 805
  • 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?
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic