Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion of a string into Double

 
pauils
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
...

}

or

(2)

Pass that string as an _expression to the the double value

example

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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about Having it generate the code to a File.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"pauils"-
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic