How to retrieve a variable name given its value?

(Java) How do I retrieve an object when given a value that coincides with one of its variables?

  • class Stuff{ //the class Stuff has 3 instance variables String name; int mass; int volume; } public class ActualProgram{ public Stuff{ //This method is of type Stuff ...show more

  • Answer:

    Keep list of object in array or list. Iterate through the objects and check value of each object with the user input. Your loop 'll like this: For ( Stuff stuff: arrayorlistOfStuffs){ If(stuff.name=="userinput"){ Print match; Break; } } Above code uses array or list , for each loop and if statements of java.

V3F5Z6UEV62PWMCO53ZBRHB4HQ at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You can create an array of such objects and then loop through the array. Stuff stuffArr [] = new Stuff [500]; stuffArr elements do NOT contain the names. They have something like a pointer. So, you need to use something like stuffArr [0] = new Stuff (); to actually create the object.

EddieJ

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.