File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes concatenate a variable to a String in a dinamic way Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "concatenate a variable to a String in a dinamic way" Watch "concatenate a variable to a String in a dinamic way" New topic
Author

concatenate a variable to a String in a dinamic way

Dura Hurtado
Ranch Hand

Joined: Feb 16, 2011
Posts: 120
Hi I am trying to do the code as follows:

for (int i = 0; i < list1.size(); i++) {
if (!content.getLoadContent$i().equals(" ")) {

¿Which code could I use to append the i var to the method?

Thanks in advance.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12926
    
    3

So, you mean that the class of content has methods named getLoadContent0, getLoadContent1, getLoadContent2 etc.?

What you want is not directly possible in Java. Sometimes people who are new to programming in Java ask a question like yours because they don't know that Java has arrays and collection classes such as ArrayList and HashMap. So, the solution often is not to create "dynamic variable names", or "dynamic method names", but to use an ArrayList or HashMap to store the data, and then iterate over that using a loop.

If you really want to call methods dynamically, by creating the name at runtime, then that is possible using the reflection API. But almost certainly a better solution is to store the data in a collection class instead of trying to do difficult things via reflection.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dura Hurtado
Ranch Hand

Joined: Feb 16, 2011
Posts: 120
Thanks for reply so quickly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: concatenate a variable to a String in a dinamic way
 
Similar Threads
can interface have abstract methods
Can a class throw exception
Calender Control;
explicit cast
for loop problem