Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Operation in String to Double conversion (http://www.velocityreviews.com/forums/t953355-operation-in-string-to-double-conversion.html)

William Lopes 10-14-2012 07:39 PM

Operation in String to Double conversion
 
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!

William Lopes 10-14-2012 09:08 PM

Re: Operation in String to Double conversion
 
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.

markspace 10-14-2012 10:59 PM

Re: Operation in String to Double conversion
 
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.




William Lopes 10-15-2012 12:45 AM

Re: Operation in String to Double conversion
 
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

Roedy Green 10-15-2012 05:49 AM

Re: Operation in String to Double conversion
 
On Sun, 14 Oct 2012 12:39:18 -0700 (PDT), William Lopes
<williamlopes.dev@gmail.com> 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.



John B. Matthews 10-15-2012 05:19 PM

Re: Operation in String to Double conversion
 
In article <3a63483c-4322-4bfb-8c28-2d528bf48443@googlegroups.com>,
William Lopes <williamlopes.dev@gmail.com> 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>

frank.asseg@gmail.com 10-15-2012 06:18 PM

Re: Operation in String to Double conversion
 
> 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.

frank.asseg@gmail.com 10-15-2012 06:27 PM

Re: Operation in String to Double conversion
 
> 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.

Lew 10-15-2012 07:40 PM

Re: Operation in String to Double conversion
 
frank...@gmail.com 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


Daniel Pitts 10-15-2012 07:51 PM

Re: Operation in String to Double conversion
 
On 10/15/12 10:19 AM, John B. Matthews wrote:
> In article <3a63483c-4322-4bfb-8c28-2d528bf48443@googlegroups.com>,
> William Lopes <williamlopes.dev@gmail.com> 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).




All times are GMT. The time now is 10:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.