-1

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

Eve
  • 514
  • 3
  • 12
  • 23
  • 1
    How many of these variables do you have, only ever a1 a2 and a3, or more ? (place your strings in an array or some collection, it'll be easier to iterate over them if you have a variable amount of strings) – nos Dec 08 '11 at 13:52
  • 3
    I'm confused as to what it is you're asking. Do you want to iterate through passing the values of a1, a2 and a3 to the `popupCmplx_RPM()` method? If so, where are the three strings stored, and what's `items`? – Anthony Grist Dec 08 '11 at 13:53
  • 1
    I'm not sure if I'm understanding your code correctly. What is "a+i" referencing to? a1,a2 and a3? – keyser Dec 08 '11 at 13:55
  • You cannot do this the way you're trying to do it. If I understand you right, you have 16 variables named `a1` through `a16`. You have a collection, `items`, which indicates which of those named variables are non-zero and should be used. You're trying to loop over `items` and concatenate the index onto the string "a" to reference a variable name, and then pass that as an argument to the method. Is that what you're trying to do? – Rob Hruska Dec 08 '11 at 14:17
  • that was the idea, but I know that doesn't works so I was wondering if people had any suggestions into solving this – Eve Dec 08 '11 at 14:33

2 Answers2

2

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);
    }
    
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • that would work if it where only three variables. But from the application down I get 16 variables/parameters some of which can be 0 so these i don't need( that's what the int items is for) – Eve Dec 08 '11 at 14:13
  • @Eve - It would be helpful to explain those sorts of things in the question. Can you update it to be more clear? – Rob Hruska Dec 08 '11 at 14:14
0

You have to parse the String as an int, and then add one to it, like

int myInt = Integer.parseInt(a) + 1;
popupCmplx_RPM(myInt);

Careful, this can throw NumberFormatException if a isn't a valid integer.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186