Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Operation in String to Double conversion

Reply
Thread Tools

Operation in String to Double conversion

 
 
William Lopes
Guest
Posts: n/a
 
      10-14-2012
Hi guys!

So, I have to do a conversion between String and Double object, but my string is a mathematical operation like "100 + 10". Even when I make a conversion using NumberFormat.getInstance of "100 + 10", my result is 100.0 only.

I would like to do it without split my string of way manually.

Someone could help me?

Thanks!
 
Reply With Quote
 
 
 
 
William Lopes
Guest
Posts: n/a
 
      10-14-2012
Em domingo, 14 de outubro de 2012 17h31min11s UTC-3, Martin Gregorie escreveu:
> On Sun, 14 Oct 2012 12:39:18 -0700, William Lopes wrote:
>
>
>
> > Hi guys!

>
> >

>
> > So, I have to do a conversion between String and Double object, but my

>
> > string is a mathematical operation like "100 + 10". Even when I make a

>
> > conversion using NumberFormat.getInstance of "100 + 10", my result is

>
> > 100.0 only.

>
> >

>
> > I would like to do it without split my string of way manually.

>
> >

>
> Sounds like you need an mathematical expression parser. I don't think
>
> there's anything suitable in the standard classes. You can:
>
>
>
> 1)see if there's a third party mathematical expression parser available
>
>
>
> 2)use a compiler generator like Coco/R
>
> http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/
>
> to create one.
>
>
>
> 3)attempt to roll your own from scratch.
>
>
>
> If it was my problem I'd work down that list in the order shown unless
>
> there are constraints set, i.e. its an assignment that you are required
>
> to solve by yourself.
>
>
>
> I've used Coco/R to generate an equivalent Java class that could handle
>
> the sort of expressions used in C preprocessor commands: it was dead easy
>
> once I understood how Coco/R works, but then again I had previously
>
> solved non-trivial problems with lex and yacc.
>
>
>
>
>
> --
>
> martin@ | Martin Gregorie
>
> gregorie. | Essex, UK
>
> org |


Thank you, using the your keywords I achieved to find a that can help me.

See: http://www.objecthunter.net/tinybo/blog/articles/86

Ps.: I didn't still tested it.

Hugs.
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      10-14-2012
On 10/14/2012 2:08 PM, William Lopes wrote:

>
> Thank you, using the your keywords I achieved to find a that can help me.
>
> See: http://www.objecthunter.net/tinybo/blog/articles/86
>
> Ps.: I didn't still tested it.



I don't think I much care for that site you link too. Too many spelling
mistakes ("i" and "java") for me to take it seriously.

Parsers aren't hard to write, and often they're used as example in
introductory texts. The C++ Programming Language by Bjarne Stroustrup,
for example, has a complete parser early in the text. Likewise Learning
Java published by O'Reilly whips up a quick algebraic parser for a demo
spreadsheet program.

Is this for a class or something? Usually if you need to evaluate
expressions there's a library that does that for you. It's really kind
of rare to need to do your own.



 
Reply With Quote
 
William Lopes
Guest
Posts: n/a
 
      10-15-2012
Em domingo, 14 de outubro de 2012 19h59min26s UTC-3, markspace escreveu:
> On 10/14/2012 2:08 PM, William Lopes wrote:
>
>
>
> >

>
> > Thank you, using the your keywords I achieved to find a that can help me.

>
> >

>
> > See: http://www.objecthunter.net/tinybo/blog/articles/86

>
> >

>
> > Ps.: I didn't still tested it.

>
>
>
>
>
> I don't think I much care for that site you link too. Too many spelling
>
> mistakes ("i" and "java") for me to take it seriously.
>
>
>
> Parsers aren't hard to write, and often they're used as example in
>
> introductory texts. The C++ Programming Language by Bjarne Stroustrup,
>
> for example, has a complete parser early in the text. Likewise Learning
>
> Java published by O'Reilly whips up a quick algebraic parser for a demo
>
> spreadsheet program.
>
>
>
> Is this for a class or something? Usually if you need to evaluate
>
> expressions there's a library that does that for you. It's really kind
>
> of rare to need to do your own.


In true the page is it http://www.objecthunter.net/exp4j/index.html
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-15-2012
On Sun, 14 Oct 2012 12:39:18 -0700 (PDT), William Lopes
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>So, I have to do a conversion between String and Double object,

but my string is a mathematical operation like "100 + 10". Even when I
make a conversion using NumberFormat.getInstance of "100 + 10", my
result is 100.0 only.
>
>I would like to do it without split my string of way manually.
>
>Someone could help me?


There is no "eval" function to parse and expressions and perform
arithmetic at compile time. To do that you need a parser or an
interpretive language like JPython.

for general conversion help see
http://mindprod.com/applet/converter.html

You can take that expression apart, with a regex if they are not too
complicated.
see http://mindprod.com/jgloss/regex.html
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.


 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      10-15-2012
In article <(E-Mail Removed)>,
William Lopes <(E-Mail Removed)> wrote:

> I have to do a conversion between String and Double object, but my
> string is a mathematical operation like "100 + 10". Even when I make
> a conversion using NumberFormat.getInstance of "100 + 10", my result
> is 100.0 only.
>
> I would like to do it without split my string of way manually.


Your distribution may already contain a suitable implementation of
javax.script.ScriptEngine:

ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory f : factories) {
System.out.println(f);
}

Selecting the available RhinoScriptEngine by extension

ScriptEngine engine = mgr.getEngineByExtension("js");
try {
System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
ex.printStackTrace(System.err);
}

prints the expected answer, 42.0.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
frank.asseg@gmail.com
Guest
Posts: n/a
 
      10-15-2012
> I don't think I much care for that site you link too. Too many spelling
> mistakes ("i" and "java") for me to take it seriously.

wow, sorry for not complying with your orthographical requirements, although imho that's hardly grounds to disregard the library.
maybe this site is more to your liking:
http://www.objecthunter.net/exp4j/
although there are probably some typos in there too
the lib may not be perfect, and i'm sure there are things that can be doneto optimize the implementation, changes i'd be happy to adapt. so take a look at the github project and drop me a line if you have some constructive criticism regarding the implementation.
 
Reply With Quote
 
frank.asseg@gmail.com
Guest
Posts: n/a
 
      10-15-2012
> I don't think I much care for that site you link too. Too many spelling
> mistakes ("i" and "java") for me to take it seriously.


wow, sorry for not complying with your orthographical requirements, although imho that's hardly grounds to disregard the library.
maybe this site is more to your liking:
http://www.objecthunter.net/exp4j/
although there are probably some typos in there too
the lib may not be perfect, and i'm sure there are things that can be doneto optimize the implementation, changes i'd be happy to adapt. so take a look at the github project and drop me a line if you have some constructive criticism regarding the implementation.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-15-2012
(E-Mail Removed) wrote:

You forgot to attribute your citation.

markspace wrote:
>> I don't think I much care for that site you link too. Too many spelling
>> mistakes ("i" and "java") for me to take it seriously.

>
> wow, sorry for not complying with your orthographical requirements, although imho that's hardly
> grounds to disregard the library.


On the contrary, it's a really reliable bellwether of product quality.

Those who are careless when you're dating are unlikely to be courteous when you're married.

If you cannot even promote a product with professionalism and attention to detail, it is very
unlikely the product will reflect a higher standard.

> maybe this site is more to your liking:
>
> http://www.objecthunter.net/exp4j/
>
> although there are probably some typos in there too
>
> the lib may not be perfect, and i'm [sic] sure there are things that can be done to optimize the
> implementation, changes i'd [sic] be happy to adapt. so take a look at the github project and drop me
> a line if you have some constructive criticism regarding the implementation.


He already gave you constructive criticism and you acted like a jerk about it. Why would anyone waste
any more time on it?

--
Lew

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-15-2012
On 10/15/12 10:19 AM, John B. Matthews wrote:
> In article <(E-Mail Removed)>,
> William Lopes <(E-Mail Removed)> wrote:
>
>> I have to do a conversion between String and Double object, but my
>> string is a mathematical operation like "100 + 10". Even when I make
>> a conversion using NumberFormat.getInstance of "100 + 10", my result
>> is 100.0 only.
>>
>> I would like to do it without split my string of way manually.

>
> Your distribution may already contain a suitable implementation of
> javax.script.ScriptEngine:
>
> ScriptEngineManager mgr = new ScriptEngineManager();
> List<ScriptEngineFactory> factories = mgr.getEngineFactories();
> for (ScriptEngineFactory f : factories) {
> System.out.println(f);
> }
>
> Selecting the available RhinoScriptEngine by extension
>
> ScriptEngine engine = mgr.getEngineByExtension("js");
> try {
> System.out.println(engine.eval("5 * 8 + 2"));
> } catch (ScriptException ex) {
> ex.printStackTrace(System.err);
> }
>
> prints the expected answer, 42.0.
>

Another library I tend to favor is OGNL, it will solve your problem
specifically, and is much more powerful. It *is* a programming language
in its own rite, so use with caution (eg, only strings you from trusted
sources).


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cast operation or string conversion? Giuseppe:G: C++ 7 07-12-2008 12:09 AM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
conversion double* to vector<double> and vice versa J.M. C++ 5 03-08-2007 03:45 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments