I am new to java and wondering how i do the following. I have a simple
program which accepts from the command line 2 numbers followed by an
operation (add, subtract, etc)
If the calculation has an associated method, how do i call the method if
i do not know it's name at runtime? All i know is that the method's name
is stored in args[3].
TIA
Anjum
--------
public static void main(String[] args) throws java.io.IOException {
String first;
String second;
String calculation;
int firstno = Integer.parseInt(args[0]);
int secondno = Integer.parseInt(args[1]);
calculation = args[3];
>> System.out.println((args[3])(firstno, secondno));
You could use reflection, methods Class.getMethod() and Method.invoke().
Class 'Method' is in package java.lang.reflect. (On the other hand,
maybe it would be easier with a simple if-chain or a switch-statement.
Depends...) /ulf