Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to Executable Code

Status
Not open for further replies.

THEMAN101

Programmer
Apr 29, 2003
44
0
0
US
I have a string variable, that holds a mathematical equation such as 4+5-6/(9+3) now i would like to be able to take this string variable and have the java compiler execute a line of code.

Such as

if i wanted to execute this command to store it in a var, but dont want to have the user pick the equation then i would do:

sum = 4+5-6/(9+3);

and sum woul equal 8.5.

now what if it was like this:

String equation;
equation = textFeild.getText();

sum = // Now what would i put here

Hope this explains it.

If you need more explination just post.
Thanks all.
 
Your going to have to change data types from string to Double or Float here is an example call to change to Double:

stringToDouble
Code:
double val = Convert.stringToDouble (digitString, defaultValue);

stringToFloat
Code:
float val = Convert.stringToFloat (digitString, (float) defaultValue);

Hope that gets you started ;)
 
Ok i looked in the java docs, and found none of that. Convert is what type of object.

Is there seriously no way to have the java compiler execute a line of code thats a string. This seems like it would be a great addition to a language.

 
>>>> Is there seriously no way to have the java compiler execute a line of code thats a string. This seems like it would be a great addition to a language.


First, the compiler does not execute code, the interpreter does.
Second, a String is a Java object, so how would you execute that ? Its not machine code is it !
There are ways of intialiasing and invoking objects on the fly in java using reflection, but this is not necessary for this.

So, for example, to show you a simple example (the algorithm to work out how to break an "equation" from a String to its operators and numeric data will be much trickier, but thats something you will have to work out.

Code:
	public int plusCalc(int a, int b) {
		return a + b;
	}

	public int minusCalc(int a, int b) {
		return a - b;
	}

 	public static void main(String args[]) throws Exception {
		String eq = "4+5";
		int i = Integer.parseInt(eq.charAt(0));
		char oper = eq.charAt(1);
		int j = Integer.parseInt(eq.charAt(2));
		
		int calc = -1;		
		
		if (oper == '+') {
			calc = plusCalc(i, j);
		} else if (oper == '-') {
			calc = minusCalc(i, j);
		}
		
	}
 
Sounds like you need a scripting language, not Java. Rather than asking for a new language feature that would be a hacker's wet dream, try perl instead...
 
Well unless you find a scripting language that will let you execute code just like that, you might just want to do a google search for code that deals with Expression Trees.
 
If you only need a calculator, you can find one in JavaWorld (author Allen Holub).

(Part 6)

sedj wrote "First, the compiler does not execute code, the interpreter does." --> So a solution would be to compile the "code" and then execute it. Can you (THEMAN101) post your code when you've worked this out ? I know it's possible ...
 
Another way to endless luck (without perl, python, ruby, ...) might be found at the project 'bsh'.
bsh stands for 'beanshell' which is a java shell, written in java and using java-syntax.
I only had a brief look at it, but I guess with some investigations you will find the right classes to invoke, since one of it's features is, to evaluate arithmetic expressions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top