0

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.

2
  • because you are print String str="str"+k; You are not using str1, str2,etc Commented Feb 4, 2014 at 6:12
  • Yes,I know it.But howw should I go round it? Commented Feb 4, 2014 at 6:12

5 Answers 5

2

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));
       }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

It almost worked..But the problem is i have 9 options(in the question i have shown only 3),and if only 3 are filled,the rest of the 6 show null values when I display.Is there a way I can add the number of options i want?
@user3040563: What type of objects are optionlist? Are they Lists, JComboboxes?
Ok i will explain.I have many questions,each question has 9 options(some might be filled some might not be filled.in the above case 3 are filled).optionlist1 is a list,so is optionlist2.....optionlist9,all are lists.optionlist1.get(i)) gets the option1 of the first question(assuming i=1),similarly optionlist2.get(i) gets the option2 of the first question and so on...
Ok i did it.check for not null or empty..thnkx
@user3040563: I have added reflection to my answer. Hope this sheds some light on your problem.
|
1

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

Comments

1

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

Comments

1

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

Comments

1

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. :)

Comments

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.