File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Execute a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Execute a string" Watch "Execute a string" New topic
Author

Execute a string

Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Hi I have trubles solving this problem
is there a way to execute together
Thanks



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Lio Liov wrote:I have trubles solving this problem

and the problem?
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
I dont know how to execute the string word.toUpper();
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

oh,
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thanks maybe I dont explain right
toUpper is going to be a String in my program(I am taking string from hash table)
So If I have the strings "word" and "toUpper()"
How can I convert word into upper cases
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
You'll have to use reflection to do that.


Joanne
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Reflection, like prayer, is the last resort of a scoundrel. You need the Strategy pattern. Create strategies for every String method you will need to call. Select a strategy based on the string you pull out of your map (this looks like a job for an enum), give the String to be worked on to the strategy, and set the strategy to work. Bam!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Dennis Deems wrote:Reflection, like prayer, is the last resort of a scoundrel.

I've been called worse
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thank you for the answer
Can you give me a few more hins
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Lio Liov wrote:Thank you for the answer
Can you give me a few more hins

About what in particular ? Dennis gave you a link. If there's something there you still don't understand, then ask a specific question.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
There are various libraries that can help with this kind of problem (executing Java code you have in string form); my favorite is Javassist.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

what i have done is to use a HashMap(String, Class with that name)


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Joanne Neal wrote:[ . . . I've been called worse
Not by me, then.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Too difficult for “beginning”. Moving discussion.
 
 
subject: Execute a string
 
Similar Threads
Question on OverLoading
Doubt in static block
Pass by value or reference?
Advice about writing a code editor!
Dynamic Binding Doubt