Reputation: 1753
I have this code
String str1 = (String) optionlist1.get(i);//str1=apple
String str2 = (String) optionlist2.get(i);//str2=mango
String str3 = (String) optionlist3.get(i);//str3=water
for(int k=1;k<4;k++){
String str="str"+k;
sb.append("<p>"+k+")"+str+"</p>");
}
When I print this I just get str1,str2,str3
instead of the actual data i.e apple,mango,water
.
Is there a way to do this ? Let me know if more info is required.
Upvotes: 0
Views: 881
Reputation: 52185
That will not work since you are stating that str = "str" + k
, which means that str
will have values of str1
, str2
, etc. These values represent string literals, and not variable names. To go round your problem you can do this:
List<String> fruit = new ArrayList<String>();
fruit.add((String) optionlist1.get(i));
fruit.add((String) optionlist2.get(i));
fruit.add((String) optionlist3.get(i));
for(String str : fruit)
{
System.out.println(str);
}
What you are trying to achieve is possible through the Reflection API
(more on that here), however I think that for your particular problem, using reflection might be a little bit overkill, so it might be better to stick with the easier solutions.
EDIT: This code should do what you are after through the use of reflection:
public static List<String> lst0 = new ArrayList<String>();
public static List<String> lst1 = new ArrayList<String>();
public static void main(String[] args) throws NoSuchElementException, NoSuchFieldException, IllegalAccessException
{
lst0.add("abc");
lst1.add("def");
Class thisClass = SOTest.class;
for(int i = 0; i < 2; i++)
{
Field field = thisClass.getField("lst" + i);
List<String> lst = (List<String>) field.get(new ArrayList<String>());
System.out.println(lst.get(0));
}
}
Upvotes: 2
Reputation: 554
str1, str2 and str3 are variable names. You are creating variable names as a String str = "str" + k which is ultimately a value not variable name.
You need to create an array of String to store your options.
String[] strs = new String[];
You can add your str1, str2.. to this array.
strs[0] = str1;
strs[1] = str2;
strs[2] = str3;
and access this array in your for loop.
for(int k=0; k < 3; k++) {
String str="str"+k;
sb.append("<p>"+ strs[k] + "</p>");
}
This will work. :)
Upvotes: 1
Reputation: 121998
Variable names cannot generate Dynamically.
You could try,
List<String> list = new ArrayList<String>();
list.add((String) optionlist1.get(i));//str1=apple
list.add((String) optionlist2.get(i));//str2=mango
list.add((String) optionlist3.get(i));//str3=water
int counter=1;
for(String str : list){
sb.append("<p>"+counter+")"+str+"</p>");
counter++;
}
Upvotes: 1
Reputation: 44824
A String by the name of str1
is not the same as a String with the value of str1
e.g.
String str1 = "Apple";
str1.equals ("str1") == false;
try using an array
String str[] = new String [3];
str[0] = (String) optionlist1.get(i);//str1=apple
str[1] = (String) optionlist2.get(i);//str2=mango
str[2] = (String) optionlist3.get(i);//str3=water
and then
for(int k=1;k<4;k++){
String s=str[k-1];
sb.append("<p>"+k+")"+s+"</p>");
}
Upvotes: 1
Reputation: 68715
you cannot reference variables using their string literal because string "str1" is not equal to reference variable str1
. One alternate approach can be to use an array as mentioned here:
String strArrr[] = new String[3];
String strArrr[0] = (String) optionlist1.get(i);//str1=apple
String strArrr[1] = (String) optionlist2.get(i);//str2=mango
String strArrr[2] = (String) optionlist3.get(i);//str3=water
for(int k=1;k<4;k++){
String str=strArr[k-1];
sb.append("<p>"+k+")"+str+"</p>");
}
Upvotes: 1