Reputation: 21
say I have three methods
method1()
method2()
method3()
and I have the user input a number corresponding to which method they want to run, is there a way to run it directly from their input? i.e. instead of having an if statement along the lines of
System.out.println("Which method would you like to run? 1/2/3");
String input = reader.readLine();
if(input == 1){method1();}
if(input == 2){method2();}
...
etc. and instead be able to have something like
System.out.println("Which method would you like to run? 1/2/3");
String input = reader.readLine();
method(input)();
?
Upvotes: 2
Views: 10467
Reputation: 1
Just to update this. Another way this can be implemented is by using switch-case in a while true loop. And using scanner object static Scanner scanner = new Scanner(System.in);
user input.
while(true){
System.out.print("Input: ");
int option = scanner.nextInt();
switch(option){
case 1 -> method1();
case 2 -> method2();
}
}
do remember to include a way to exit the while loop in any of the methods.
Upvotes: 0
Reputation:
Yes you could achieve that by using an interface as follows:
interface A {
void run();
}
public void method1() {}
public void method2() {}
public void mainMethod(String[] args) {
// Initialise the method map - note, you only have to do this once
// So, this initialisation code can go into a constructor
// And mothodMap can be declared as a final instance variable.
A methodOne = new A() { @Override public void run() { method1(); } };
A methodTwo = new A() { @Override public void run() { method2(); } };
Map<Integer, A> methodMap = new HashMap<>();
methodMap.put(1, methodOne);
methodMap.put(2, methodTwo);
Integer input = /* get it from user*/ 1;
A aMethod = methodMap.get(input);
aMethod.run();
}
Upvotes: 8
Reputation: 26228
No, not unless you use reflection. Java doesn't have function pointers, otherwise you could index to the appropriate function in an array. But what's wrong with if
statements? They're more readable and secure..
If you're looking for a future-proof, more abstract solution, consider a strategy pattern:
// strategy
interface CommandMethod {
void runMethod();
}
// for every method 1 .. n
class CmdMethod1() implements CommandMethod {
void runMethod() {
// concrete implementation
}
}
// initialization ----------------
Map<String, CommandMethod> cmds = new HashMap<String, CommandMethod>();
cmds.put("1", new CmdMethod1());
// .. etc ..
cmds.put("n", new CmdMethodN());
// at the prompt:
System.out.println("Which method would you like to run? 1/2/3/.../n");
String input = reader.readLine();
cmds.get(input).runMethod(); // more like what you're going for ?
Upvotes: 4
Reputation: 86774
The standard way of accomplishing this is to create a "functor"
public interface Functor
{
public void execute();
}
public class Method1 implements Functor
{
public void execute() { /* do something */ }
}
etc...
private Functor[] f = { new Method1(), new Method2(), new Method3() };
...
// Execute the method selected by i
f[i].execute();
Also take a look at the Callable
interface
Upvotes: 0
Reputation: 1660
There's no way to do this. However, you can use if-statements or switch-case statements to make the "redirection" process less cumbersome. You might also consider creating a wrapper function to accept the user input to make your code cleaner.
Upvotes: 0
Reputation: 5782
Not without having an if or switch statement (or reflection like paislee pointed out). If you wanted to do something like method(input);
you would need the if/switch statement in another method:
....
String input = reader.readLine();
method(input);
}
private void method(int input) {
if (input == 1) {method1();}
if (input == 2) {method2();}
}
Upvotes: 0