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

6
  • Possible duplicate of How do I compare strings in Java? Commented Nov 5, 2016 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. Commented Nov 5, 2016 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. Commented Nov 5, 2016 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)) Commented Nov 5, 2016 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 Commented Nov 5, 2016 at 14:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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;
}

2 Comments

Seems asker have no clear idea of 'variable', like Your (correct) answer show. Must distinct instance field, static field, local etc
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.