File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Conversion of a string into Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Conversion of a string into Double " Watch "Conversion of a string into Double " New topic

Conversion of a string into Double


Joined: Nov 07, 2004
Posts: 1
I want to convert the string as a double value

String =i*i+2;

for (int i=0;i<100;i++)
double y= String ;// some polynomial equation




Pass that string as an _expression to the the double value


for (int i=0;i<100;i++)
double y= i*i+2 ;// where i*i+2 is an _expression (which has been derived from a string)


Is it possible to do that

Can Somebody help me out in this.....
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There's nothing built into the langauge to evaluate an expression. You could probably google and find some libraries that will do it. To do it yourself you have to parse the string into tokens and interpret the operators. I did it in COBOL once for fun, but not in Java. Any recent computer science grad should have this in their class notes. If that's the path you want to take, maybe somebody will post some hints.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
r phipps
Ranch Hand

Joined: Sep 14, 2004
Posts: 60
What about Having it generate the code to a File.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I suppose you could generate a small Java source String and use the compiler to generate a class file. At worst you'd have to write the source to a file and call the compiler via Runtime.exec().
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

Two things for you....

1st thing:
Welcome to the JavaRanch! We like to keep a professional lookin' image around here, and don't really fancy it when people try to show up the one-eyed moose. So if you could change your display name (click here), that'd be just dandy. (If you'd like a more thorough reason as to why I'm asking you to change your name, check out the Naming Policy.)

2nd thing:
Looks like this is the same question as this one. So you might want to check out that thread to get some more insight. Guess you're both in the same class??

Good luck! And again, Welcome to the JavaRanch!
I agree. Here's the link:
subject: Conversion of a string into Double
jQuery in Action, 3rd edition