aspose file tools*
The moose likes Beginning Java and the fly likes Running syntax stored in String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running syntax stored in String" Watch "Running syntax stored in String" New topic
Author

Running syntax stored in String

Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
I'm not sure if this is a basic question or advanced. I apologize if I'm on the wrong forum.

In my code execution, I end up having some syntax that gets stored in a string variable such as





My question is how do I execute the code stored in the variable strName. I've been googling it for a while but didn't really find any fix for this.

Any help/guidance is appreciated.

Thanks in advance.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The short answer is that it is possible to do what you want using reflection (google java reflection).

The longer answer is this is almost always the wrong way to go about doing anything. Exactly what problem are you trying to solve here? Maybe we can help you find a more idiomatic way.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
Garrett Rowe wrote:The short answer is that it is possible to do what you want using reflection (google java reflection).

The longer answer is this is almost always the wrong way to go about doing anything. Exactly what problem are you trying to solve here? Maybe we can help you find a more idiomatic way.


Hi Garrett,

I'm actually working with graph data structures. The thing is that every head and tail node has a name attached to it.

So suppose I have an edge with nodes named "A" and "B", then there will be a double array variable called "AB" that will contain the weight of the edge at a certain index.

Now for a large graph, I can't do switch or if statements to see what the name will be as there are numerous possible combinations.

However, I can make a string such as below




That is why I'm trying to run the command stored in the string variable executeCommand.

Does that clarify the problem a bit?

Again, thanks a lot for your help.
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
We almost never need to invoke a String constructor (new String()). So, instead of

write this instead:
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'm still not sure I understand exactly what you're doing, but as a start you could use a Map<String, double[]> or better yet Map<String, List<Double>> and look up the values by the String name:



Although I'm not really sure that String's are what you want to be working with here. A more idiomatic (and type safe) way would be to create classes that more effectively model the actual domain:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

+1 to Garrett--so far I don't see any reason to make this anywhere near as "dynamic" as you're trying to make it (and if it *does* need to be like that I'd probably recommend against Java, at least for that portion of your app).

Maybe you could explain further why you're doing it the way you're trying to do it?
Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
Thanks for your response David.

Well, I wasn't sure how else I could do it. I'll explain the problem below. I really like the idea of Maps<> and I'm looking into it. Thanks Garrett.

=======================================================================================

I am given a specified set of nodes. Lets call them {A, B, C, D}. I have to make a graph from this. <-- the name of the nodes is all that will be specified to me.

I have a bunch of data that I need to read in. I plan to store it in separate double array variables such as AB[], AC[], BA[], BB[], etc. (these are just example names and not the actual names).
I read in data into it from file "AB.txt", "AC.txt", etc. (assume that files will always exist)

Then when I make a graph, to assign/update cost of each edge, if the edge is AB, then I use the array AB[index].

========================================================================================

Since all I'm given initially is the names of the nodes, I was planning on making a string to make the variable names and then execute the string commands. Does that explain the problem better? I know its a lousy way to do it but I wasn't sure how else I could do this.




David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd say it sounds like a map is what you want then, plus node (and potentially edge) classes. The map would just be for convenience, used for accessing nodes quickly while adding each's edges.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running syntax stored in String