Java java java java :(

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
Have come up with another problem of sorts; I know a way around it but with my program running as it is, it turns 2lines of code into 45 so I'd rather do things this way if I can just learn the syntax.

Can't really think of a simple way of explaining it so I might babble a bit. What I'm trying to do is call a method from a String. It's not an issue of inheritance or whatever, I mean for example, I have a method called 'JoeBloggs' and a method called 'JillBloggs'. If for example the user had a radiobutton to select one of those and on clicking OK, it'd run one of those methods. The normal way of doing it would be to say that if joebloggs is pressed, run this etc etc but I don't want that. I want the selected component(got it inputted as a String) to run the method called the same thing. So using the String JoeBloggs would attempt to call the JoeBloggs method.

Erm...to try and further clarify, I'll put some code here:

Code:
String temp = inputStream.readLine();
//Insert calling code here

    public String getLevel1easy1() {
        return level1easy1;
    }
   public String getLevel2easy1() {
        return level1easy1;
    }

So I now have a String called 'getLevel1easy1' and I wish any existing method called that to run. You can see the problem I have when there are a few levels...and a few difficulties.

In fact, I've jsut noticed a better way of doing this would be with a dynamic 3dimensional(maybe 4 :()ListArray but I'm still interested as to how I could do this for future reference and all :)

Are there any decent java type people who would be willing to add me on MSN for occasional questions? :D
 

liloe

It's my birthday today!
Joined
Jan 25, 2004
Messages
4,166
So you mean if you do a CurrentLevel = getLevel1easy1() it does not work?

Oh and yes, doing a method for each level/diff is waaay complicated. Just do a level and a diff table as array
 

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
It would work. What I was wondering was say for example I had a String called 'temp'. The attribute of temp at the moment is 'level1' for example. I'm wondering if there is a direct way of calling the Strings attribute rather then naming it specifically to call a method.

So for example.... score = temp.getAttribute().runMethod();? Or something along those lines (don't think that syntax specifically exists). I'm trying to call get methods hence the 'score = ' at the beginning but I was just wondering if you can call a method without specifically saying you want 'x' method, instead by saying you want to call a method which is named whatever the Strings attribute currently is.

Does this make any sense at all? Sorry I'm being very vague I just simply cannot think of a way to explain it well. It was something I tried to do about a year ago but wasn't far advanced enough but by now, I'm trying to investigate as much as I possibly can, especially something like this where I can see a good long term use for it.

Cheers for the reply.
 

liloe

It's my birthday today!
Joined
Jan 25, 2004
Messages
4,166
Hm, as far as I get your explanation, what you want to do is, that once you assign something to the "temp" in your example, you want it as base for calling methods?

I think then you have to make it a new object, iirc. Been more than a year that I haven't done any java, but I think then you'll have to work with objects. So something like (and don't hold me down on this!!)

String temp = newString()
{
->here all your methods PLUS the value variable
}

Then you could call methods from temp and if you need a new object, you can simply clone it....not sure if I got you, though =)
 

Users who are viewing this thread

Top Bottom