0

Hello
I need help with a program
I want to make a method that takes a String and uses the text in that String to use a method from an object.
Something like this:

public void doThat(String text) {
myObject.text();// Where text is the actual text stored in the String variable
}
Help, please? :)

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

well the best ting is by creating it in the method that will use it, for example if myObject() is to use the String text, then why not call it inside myObject as myObject(text). it would save you space and time

0

This has a lot in common with your very recent thread http://www.daniweb.com/software-development/java/threads/390417
It depends on whether the possible text/methods are known at compile time or not.
If they are known then just use a switch (Java 7) to call the appropriate method depending on the text. There is an elegant variation on this using enums, if you're interested.
If they are not known at compile time then you have to use reflection to get the Method object corresponding to the text name, and "invoke" the Method.
Which case is it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.