Search code examples
javafor-looprft

How to merge two strings into another variable


The following is the situation:

I have three String parameters a1,a2,a3

Each of the parameter has a different number inside

a1: 12

a2: 34

a3: 56

So using a for -loop i want to insert these number into a method

items = the amount of parameters so in this case 3

 for (int i=1;i<=items;i++){
    popupCmplx_RPM(a+i);
    sleep(2);
    }

So the problem is if I run the functionality it will create for the String a1: a+i -> 121 instead of 12

The parameters are already set I can't change that part of the code so help is appreciated. I sure there is an easier way without the parameters, but other that adding new code I can't remove those

The total amount of parameters set at the moment are 16 some of which can be 0 so in this example there are only three and the rest are zero. with the int items variable the amount of parameters used is given


Solution

  • It looks like you're looping and trying to use the index of the loop to reference a variable. You cannot do that (without reflection) in Java.

    (If that's an incorrect interpretation of your question, please update it to clarify.)

    You probably have a couple options:

    1. Just reference the variables without looping:

      popupCmplx_RPM(a1);
      sleep(2);
      popupCmplx_RPM(a2);
      sleep(2);
      popupCmplx_RPM(a3);
      sleep(2);
      
    2. Store the values in a collection instead of individual variables:

      List<Integer> list = new ArrayList<Integer>();
      list.add(12);
      list.add(34);
      list.add(56);
      
      for(Integer value : list) {
          popupCmplx_RPM(value);
          sleep(2);
      }