Create methods from array
Hello all,
I want to know if I can create methods from writing them by using a for loop?
I think I should use setmethod, but I can't find an example that I understand - yes I'm a newbie :-). So if anyone can point me to the right direction! Here is the code;
class commands{
String [] commandos ={"open","play","exit","help"};
int i, z;
String str =new String();
InputStreamReader reader =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(reader);
public commands(){
/* methods I want to implement
for (z=0; z < commandos.length; z++) {
public void commandos[z]() { // syntax I want to implement
System.out.println("Commando " + commandos[z] + " is being executed");
}}
*/
try{
do{
str = br.readLine ();
for (i=0; i < commandos.length; i++){
if (str.toLowerCase().equals (commandos[i])){
System.out.println("Commando " + commandos[i] +" is being executed");// needs replacement
}}
}while (!str.toLowerCase().equals ("quit"));
}
catch (IOException e){ System.out.println("IOexception = " + e );}
}
}
publicclass comecho{
publicstaticvoid main (String [] args){
commands com =new commands();
}
}
[2724 byte] By [
Nuawana] at [2007-11-27 0:48:33]

No, methods can't be written that way.
To archieve what you're trying to do, you either can:
main(){
for(int i=0;i<count;i++){
if(condition) print(i);
}
}
void print(int i){
System.out.println(i+" is being printed");
}
OR
main(){
print();
}
void print(){
for(int i=0; i><count;i++){
if(condition) System.out.println(i+" is being printed");
}
}
>
Thanks for the quick reply. Bummer it can't be done that though. I'll live with it.
I slightly changed the code to see if I can achieve the following.
I want to call one function when array value equals a method name. Is this possible?
import java.io.*;
class commands {
String [] commandos = {"open", "play", "exit", "help"};
int i, z;
String str = new String();
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
public commands() {
// public void open() { }
// public void play() { }
// public void exit() { }
// public void help() { }
try {
do {
str = br.readLine ();
for (i=0; i < commandos.length; i++) {
if (str.toLowerCase().equals (commandos[i])) {
System.out.println("nothing"); // How to convert commandos[i] to call function commandos[i]
}}
} while (!str.toLowerCase().equals ("quit"));
}
catch (IOException e) { System.out.println("IOexception = " + e );}
}
}
public class comecho {
public static void main (String [] args) {
commands com = new commands();
}
}
You can use HashMap - key is string (e.g. "open"), value is Object e.g. new Open()
All commands objects must implement one interface with one (or more) method (s). For example: public void run()
. When type command from input just get object that correspond to that command from the Map and call it's Run method: map.get(str).run();
For each command you must write class instead of method and put your logic in run() method of a new class.