• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assign value to a variable inside for loop

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have declared 8 strings as below.
I want to assign value to each variable inside a for loop. I tried as below but, it is showing errors. I need tehse values as string values to be displayed in a jsf page.



I get the error this.Name_[i]. Can anyone please help to resolve this issue. Thanks.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I suspect that you could be using 'this' inside a static method.
What error do you get?
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following error.

Can not file symbol Name_

I am trying to assign
Name_1 = "str1";
Name_2 = "str2";
Name_3 = "str3";
Name_4 = "str4";
Name_5 = "str5";
Name_6 = "str6";
Name_7 = "str7";
Name_8 = "str8";

I am trying to do the above inside a for loop which fails...


 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this.Name_[i] indicates an Array, where as you have declared as Name_1 etc as String variables. The compiler is (rightly) complaining that it cannot find Name_ because no such variable exists.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, Thanks Harsha. It works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic