aspose file tools*
The moose likes Beginning Java and the fly likes Assign value to a variable inside for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assign value to a variable inside for loop" Watch "Assign value to a variable inside for loop" New topic
Author

Assign value to a variable inside for loop

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
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

Joined: Mar 02, 2005
Posts: 443
    
    1


I suspect that you could be using 'this' inside a static method.
What error do you get?
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
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

Joined: Jul 18, 2011
Posts: 287
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Great, Thanks Harsha. It works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assign value to a variable inside for loop