aspose file tools*
The moose likes Java in General and the fly likes Calling methods in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling methods in a loop" Watch "Calling methods in a loop" New topic
Author

Calling methods in a loop

Mervin Chan
Greenhorn

Joined: Dec 03, 2004
Posts: 24
I have a problem. If i have to call a bunch of methods with similar names like

String rec1 = Records.getRecord1();
String rec2 = Records.getRecord2();
String rec3 = Records.getRecord3();

is there a way I can do this in a loop?

for example:

for(int count=1;count<4;count++){

rec//count// = Records.getRecord//count//();
}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

No, not if the records are named getRecordN (where N is 1, 2, 3, ...). (Well, you can do it via reflection, but...).

Note that the design of the Records class should have been done differently: Instead of a whole bunch of getRecordN methods, there should have been just one getRecord(int number) method; then it would be easy:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

(Well, you can do it via reflection, but...).


Okay, I'm bored. Let's do this with reflection. I guess if you have this...



You can do this...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling methods in a loop
 
Similar Threads
Strings
remove "\n"s from a String and count them
variable problem
C and Java
search files in directory for string and return count