0

I have two variables that I'm trying to compare but the variable names are long and I'm trying to clean up my code, all the code here is just used as an example.

What I want to do is something like this:

if(objectOne.objectTwo.variableName1 == objectTwo.objectTwo.variableName1)
if(objectOne.objectTwo.variableName2 == objectTwo.objectTwo.variableName2)
...

and do this multiple times but every time change the number at the end of the string but I'm trying to do it like this:

for(int i = 0 ; i < 5 ; ++i) {
   String firstString = "objectOne.objectTwo.variableName" + i;
   String secondString = "objectTwo.objectTwo.variableName" + i;
   if(firstString == secondString)
      //more code
}

however this compares the Strings and I'm trying to use the Strings themselves as references to different variables is there any way of doing this?

EDIT: I'm looking to clean up the code but the main problem I'm having is if I had 100 variableNameNumber variables I would have to do 100 separate if statements, I'm trying to do it in a simple for loop as i increments the variable names get updated

w13rfed
  • 355
  • 5
  • 12
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Tim Biegeleisen Nov 05 '16 at 13:34
  • 7
    It sounds like really you should be using collections or arrays. Having multiple variables with a suffix of 1, 2, 3, 4 etc is usually a code smell. – Jon Skeet Nov 05 '16 at 13:35
  • Also, you could at least use `Foo a = objectOne.objectTwo; Foo b = objectTwo.objectTwo;`and then compare `a.variableName1` with `b.variableName1`. Note that using public fields is also a code smell. – JB Nizet Nov 05 '16 at 13:37
  • If all the variables you are trying to compare are in the same object you can override the `equals()` method in that object, and compare the variables in the method, then you test only the equality of the two objects : `if(objectOne.objectTwo.equals(objectTwo.objectTwo))` – Younes HAJJI Nov 05 '16 at 13:54
  • The suffixes of numbers is part of a specification unfortunately but thanks for the heads up, the main problem i'm having isn't shortening the variable names it's making the for loop work using the i variable as part of the string so if I had 100 statements to do for example I wouldn't have to do 100 lines – w13rfed Nov 05 '16 at 14:01
  • Also I think arrays would work very well but unfortunately again it's part of a specification and I need many separate variables – w13rfed Nov 05 '16 at 14:02

2 Answers2

1

It's possible to do with a Map as long as variables name are unique(of course it must be) , as follows:

Map<String, String> args = new HashMap<String, String>();
args.put("objectOne.objectTwo.variableName1", objectOne.objectTwo.variableName1);
args.put(...);
.
.
.
for(int i = 0 ; i < 5 ; ++i) {
   String firstString = "objectOne.objectTwo.variableName" + i;
   String secondString = "objectTwo.objectTwo.variableName" + i;
   if(args.get(firstString) == args.get(secondString))
      //more code
}

However, the motivation can be skeptical, as Jon Skeet points out.

Jerry Chin
  • 657
  • 1
  • 8
  • 25
0

You need using java Reflection API here to get the real field value for "objectOne.objectTwo.variableName1", "objectOne.objectTwo.variableName2" and "objectTwo.objectTwo.variableName1", "objectTwo.objectTwo.variableName2"

Here is the example Get a variable value from the variable name

import java.lang.reflect.Field;

public class Main {
  public static void main(String[] args) throws Exception {
    Object clazz = new TestClass();
    String lookingForValue = "firstValue";

    Field field = clazz.getClass().getField(lookingForValue);
    Class clazzType = field.getType();
    if (clazzType.toString().equals("double"))
      System.out.println(field.getDouble(clazz));
    else if (clazzType.toString().equals("int"))
      System.out.println(field.getInt(clazz));

    //System.out.println(field.get(clazz));
  }
}

class TestClass {
  public double firstValue = 3.14;
}
Liping Huang
  • 4,378
  • 4
  • 29
  • 46
  • Seems asker have no clear idea of 'variable', like Your (correct) answer show. Must distinct instance field, static field, local etc – Jacek Cz Nov 05 '16 at 13:58
  • I'm new to Java so just trying to get my head around the answer but Jacek what do you mean must distinct instance field? – w13rfed Nov 05 '16 at 14:06