Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19332 > unrolled thread

Operation in String to Double conversion

Started byWilliam Lopes <williamlopes.dev@gmail.com>
First post2012-10-14 12:39 -0700
Last post2012-10-17 16:54 -0400
Articles 20 on this page of 23 — 12 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Operation in String to Double conversion William Lopes <williamlopes.dev@gmail.com> - 2012-10-14 12:39 -0700
    Re: Operation in String to Double conversion Martin Gregorie <martin@address-in-sig.invalid> - 2012-10-14 20:32 +0000
      Re: Operation in String to Double conversion William Lopes <williamlopes.dev@gmail.com> - 2012-10-14 14:08 -0700
        Re: Operation in String to Double conversion markspace <-@.> - 2012-10-14 15:59 -0700
          Re: Operation in String to Double conversion William Lopes <williamlopes.dev@gmail.com> - 2012-10-14 17:45 -0700
          Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 11:18 -0700
            Re: Operation in String to Double conversion Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-10-16 00:14 +0200
              Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 15:41 -0700
          Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 11:27 -0700
            Re: Operation in String to Double conversion Lew <lewbloch@gmail.com> - 2012-10-15 12:40 -0700
              Re: Operation in String to Double conversion frank.asseg@gmail.com - 2012-10-15 14:24 -0700
                Re: Operation in String to Double conversion Lew <lewbloch@gmail.com> - 2012-10-15 14:48 -0700
                Re: Operation in String to Double conversion Arved Sandstrom <asandstrom2@eastlink.ca> - 2012-10-15 19:14 -0300
                  Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 16:58 -0400
                Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 17:02 -0400
              Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 17:13 -0400
                Re: Operation in String to Double conversion Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-17 17:32 -0400
    Re: Operation in String to Double conversion Roedy Green <see_website@mindprod.com.invalid> - 2012-10-14 22:49 -0700
    Re: Operation in String to Double conversion "John B. Matthews" <nospam@nospam.invalid> - 2012-10-15 13:19 -0400
      Re: Operation in String to Double conversion Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-10-15 12:51 -0700
        Re: Operation in String to Double conversion "John B. Matthews" <nospam@nospam.invalid> - 2012-10-16 09:39 -0400
    Re: Operation in String to Double conversion Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-15 20:47 -0400
    Re: Operation in String to Double conversion Arne Vajhoej <arne@vajhoej.dk> - 2012-10-17 16:54 -0400

Page 1 of 2  [1] 2  Next page →


#19332 — Operation in String to Double conversion

FromWilliam Lopes <williamlopes.dev@gmail.com>
Date2012-10-14 12:39 -0700
SubjectOperation in String to Double conversion
Message-ID<3a63483c-4322-4bfb-8c28-2d528bf48443@googlegroups.com>
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!

[toc] | [next] | [standalone]


#19333

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2012-10-14 20:32 +0000
Message-ID<k5f7gf$p6g$2@localhost.localdomain>
In reply to#19332
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       |

[toc] | [prev] | [next] | [standalone]


#19334

FromWilliam Lopes <williamlopes.dev@gmail.com>
Date2012-10-14 14:08 -0700
Message-ID<b9f3c86c-4871-45d0-914d-6b345667d892@googlegroups.com>
In reply to#19333
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.

[toc] | [prev] | [next] | [standalone]


#19335

Frommarkspace <-@.>
Date2012-10-14 15:59 -0700
Message-ID<k5fg4d$nu6$1@dont-email.me>
In reply to#19334
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.


[toc] | [prev] | [next] | [standalone]


#19337

FromWilliam Lopes <williamlopes.dev@gmail.com>
Date2012-10-14 17:45 -0700
Message-ID<2958d6bb-8527-4b5c-a113-4492fdaebf42@googlegroups.com>
In reply to#19335
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

[toc] | [prev] | [next] | [standalone]


#19367

Fromfrank.asseg@gmail.com
Date2012-10-15 11:18 -0700
Message-ID<afe33c9e-8aff-4fbe-afbc-98f51e2bc26e@googlegroups.com>
In reply to#19335
> 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 done to 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.

[toc] | [prev] | [next] | [standalone]


#19385

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-10-16 00:14 +0200
Message-ID<k5i1sc$8ei$1@dont-email.me>
In reply to#19367
On 15/10/2012 20:18, frank.asseg@gmail.com allegedly 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.
> 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 done to 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.

You are the author that lib, right? Then allow me, for what it is worth,
to opine with markspace -- not only, incidentally, with his opinion, but
more importantly and fundamentally, with his approach.

/Le style, c'est l'homme/, as the saying goes. Style matters. Not so
much in and of itself, but to the extent of what it tells you about the
author. It doesn't tell you everything, and it is not always right, but
it is so more often than not. And most importantly, it allows you to
come to an at least preliminary conclusion in the face of a limited set
of data.

Being able to make quick decisions is a crucial skill, as I am sure
you'll realise, or would after giving it minimal thought. You need means
to separate the wheat from the chaff -- the quicker you are able to do
this, the more productive you can be. When choosing a library, you
rarely have the luxury of analysing it in every last detail. Especially
if it performs a fairly common, and merely cumbersome, task, picking it
apart might take longer than just writing the damn thing yourself.

In such a situation, you need to discriminate effectively; you need
effective discriminators. Good-will and trust -- say, if it's a library
published by someone whose other works you've used to your satisfaction
-- can be one such discriminator, and perhaps the most important one.
Failing that, or in conjunction with that, apparent style (code style,
but also, as they're related, lexical and overall style) is perhaps the
second most important one.

That being said, your grammar and, if you'll pardon my saying so, the
slight snottiness you display here notwithstanding, the code examples on
your page look fairly okay, and if I were the OP, and didn't have the
alternative of using the scripting engine (or if my task required
functions and the other more advanced features you offer), I'd probably
give your lib a good second look.

My two cents. Please don't flame me.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#19387

Fromfrank.asseg@gmail.com
Date2012-10-15 15:41 -0700
Message-ID<eb49e6db-ae18-44bd-a6c2-c0006780216d@googlegroups.com>
In reply to#19385
On Tuesday, October 16, 2012 12:14:36 AM UTC+2, Daniele Futtorovic wrote:


> [...] and if I were the OP, and didn't have the
> alternative of using the scripting engine (or if my task required
> functions and the other more advanced features you offer), I'd probably
> give your lib a good second look.
the jsr223 scripting engine works fine, but is rather slow, this is the comparison with pure java math, livetribe's jsr223 implementation and exp4j from the tests on my local desktop machine:

expression		log(x) - y * (sqrt(x^cos(y)))
exp4j			1123189 [561.59k calc/sec]
Java Math		4590231 [2295.12k calc/sec]
JSR 223(Javascript)	1674 [837.0 calc/sec]

This of course is a very synthetic benchmark, but be aware that the scripting engine can be quite slow when performing a lot of operations, and a lot of performance is to be gained with a pure java implementation. and this argument does neither include the time to warm up the script engine nor the increased memory footprint.

[toc] | [prev] | [next] | [standalone]


#19370

Fromfrank.asseg@gmail.com
Date2012-10-15 11:27 -0700
Message-ID<9ac3b7f9-5751-410f-bac3-50703b976b6d@googlegroups.com>
In reply to#19335
> 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 done to 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. 

[toc] | [prev] | [next] | [standalone]


#19376

FromLew <lewbloch@gmail.com>
Date2012-10-15 12:40 -0700
Message-ID<ba872cf4-ed48-489b-a4fb-3b7f2018dbfe@googlegroups.com>
In reply to#19370
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

[toc] | [prev] | [next] | [standalone]


#19381

Fromfrank.asseg@gmail.com
Date2012-10-15 14:24 -0700
Message-ID<fbfe4f3c-61a8-4318-9961-8bc67580f6a6@googlegroups.com>
In reply to#19376
On Monday, October 15, 2012 9:40:04 PM UTC+2, Lew wrote:
> If you cannot even promote a product with professionalism and attention to 
> detail, it is very unlikely the product will reflect a higher standard.
again i beg to differ. the code quality has got nothing to do with the manner an article about the library is written. i think judging a developer's skill based on his professionalism as an editor is like saying a monkey can't climb because he sucks at flying.

> He already gave you constructive criticism and you acted like a jerk about it. 
well i certainly didn't call him a jerk for having a different opinion. so in order not to succumb to godwin's law i won't respond to any more posts regarding the manner in which i choose to write.

[toc] | [prev] | [next] | [standalone]


#19382

FromLew <lewbloch@gmail.com>
Date2012-10-15 14:48 -0700
Message-ID<8f3e19c3-e015-4c0a-b8f5-015fd5c9ffbf@googlegroups.com>
In reply to#19381
frank...@gmail.com wrote:
>Lew wrote:
>> If you cannot even promote a product with professionalism and attention to 
>> detail, it is very unlikely the product will reflect a higher standard.
> 
> again i [sic] beg to differ. the code quality has got nothing to do with the manner an article about the
> library is written. i think judging a developer's skill based on his professionalism as an editor is like 
> saying a monkey can't climb because he sucks at flying.

You can make up all the cute similes you like, but my experience is solid in this area.

Illiterate descriptions rarely correspond to quality products.

>> He already gave you constructive criticism and you acted like a jerk about it. 
> 
> well i [sic] certainly didn't call him a jerk for having a different opinion. 
> so in order not to succumb to godwin's law i [sic] won't respond to any more posts regarding the 
> manner in which i [sic] choose to write.

Good luck with that attitude. You'll need it.

And you not calling him a jerk didn't mean you didn't act like one. Your whole attitude is 
resentful and hostile despite being given good advice. 

No one but you cares about your ego.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#19384

FromArved Sandstrom <asandstrom2@eastlink.ca>
Date2012-10-15 19:14 -0300
Message-ID<MU%es.26678$sB3.20536@newsfe05.iad>
In reply to#19381
On 10/15/2012 06:24 PM, frank.asseg@gmail.com wrote:
> On Monday, October 15, 2012 9:40:04 PM UTC+2, Lew wrote:
>> If you cannot even promote a product with professionalism and attention to
>> detail, it is very unlikely the product will reflect a higher standard.
> again i beg to differ. the code quality has got nothing to do with the manner an article about the library is written. i think judging a developer's skill based on his professionalism as an editor is like saying a monkey can't climb because he sucks at flying.
>
>> He already gave you constructive criticism and you acted like a jerk about it.
> well i certainly didn't call him a jerk for having a different opinion. so in order not to succumb to godwin's law i won't respond to any more posts regarding the manner in which i choose to write.
>
>
The way I see it, the project page is fine. The article does have 
grammar and spelling problems to the extent that I also, like Lew, 
started to question the code...*until* I noticed that the author 
(yourself) is from SW Germany.

This is one of the first things I do when I see that software 
documentation and/or related writings have grammar and spelling problems 
- I check to see if English is the author's first language.

AHS

[toc] | [prev] | [next] | [standalone]


#19418

FromArne Vajhoej <arne@vajhoej.dk>
Date2012-10-17 16:58 -0400
Message-ID<507f1bd9$0$282$14726298@news.sunsite.dk>
In reply to#19384
On 10/15/2012 6:14 PM, Arved Sandstrom wrote:
> On 10/15/2012 06:24 PM, frank.asseg@gmail.com wrote:
>> On Monday, October 15, 2012 9:40:04 PM UTC+2, Lew wrote:
>>> If you cannot even promote a product with professionalism and
>>> attention to
>>> detail, it is very unlikely the product will reflect a higher standard.
>> again i beg to differ. the code quality has got nothing to do with the
>> manner an article about the library is written. i think judging a
>> developer's skill based on his professionalism as an editor is like
>> saying a monkey can't climb because he sucks at flying.
>>
>>> He already gave you constructive criticism and you acted like a jerk
>>> about it.
>> well i certainly didn't call him a jerk for having a different
>> opinion. so in order not to succumb to godwin's law i won't respond to
>> any more posts regarding the manner in which i choose to write.
>>
>>
> The way I see it, the project page is fine. The article does have
> grammar and spelling problems to the extent that I also, like Lew,
> started to question the code...*until* I noticed that the author
> (yourself) is from SW Germany.
>
> This is one of the first things I do when I see that software
> documentation and/or related writings have grammar and spelling problems
> - I check to see if English is the author's first language.

In general I agree.

But "java" does not look good in German either.

Arne

[toc] | [prev] | [next] | [standalone]


#19420

FromArne Vajhoej <arne@vajhoej.dk>
Date2012-10-17 17:02 -0400
Message-ID<507f1cdd$0$282$14726298@news.sunsite.dk>
In reply to#19381
On 10/15/2012 5:24 PM, frank.asseg@gmail.com wrote:
> On Monday, October 15, 2012 9:40:04 PM UTC+2, Lew wrote:
>> He already gave you constructive criticism and you acted like a
>> jerk about it.
> well i certainly didn't call him a jerk for having a different
> opinion.

You got criticism and you replied with sarcastic comments.

So you demonstrated that you are not willing to listen to
criticism and attack those that provide such.

Arne

[toc] | [prev] | [next] | [standalone]


#19421

FromArne Vajhoej <arne@vajhoej.dk>
Date2012-10-17 17:13 -0400
Message-ID<507f1f60$0$294$14726298@news.sunsite.dk>
In reply to#19376
On 10/15/2012 3:40 PM, Lew wrote:
> frank...@gmail.com wrote:
>> 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.
>
...
>
> If you cannot even promote a product with professionalism and attention to detail, it is very
> unlikely the product will reflect a higher standard.

If one has a product and want to promote it, then the description
should have a certain level of professionalism.

But if one has written some code for fun or for own usage and are
willing to share it, but not have any intentions of making it widely
used, then you really can't expect much.

Arne

[toc] | [prev] | [next] | [standalone]


#19425

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-10-17 17:32 -0400
Message-ID<k5n861$2l1$1@dont-email.me>
In reply to#19421
On 10/17/2012 5:13 PM, Arne Vajhoej wrote:
> [...]
> If one has a product and want to promote it, then the description
> should have a certain level of professionalism.

     I see no evidence that the author is in any way "promoting"
the code.  He did not join this thread until after someone else
had posted a link to his blog page, and until yet another person
had criticized the writeup.  On the blog itself, we find

	exp4j is currently in development and
	not recommended for production use.

... in boldface, on a line all by itself, made more prominent
by ample vertical white space.  If that's "promotion," I'd hate
to see his idea of "reticence!"

     Ease up, people.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#19351

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-10-14 22:49 -0700
Message-ID<cv8n78pc07lcpfmld43oj93ecj55e8409d@4ax.com>
In reply to#19332
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. 

[toc] | [prev] | [next] | [standalone]


#19365

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-10-15 13:19 -0400
Message-ID<nospam-64C7D2.13194715102012@news.aioe.org>
In reply to#19332
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>

[toc] | [prev] | [next] | [standalone]


#19377

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-10-15 12:51 -0700
Message-ID<3PZes.19616$zI3.16882@newsfe18.iad>
In reply to#19365
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).

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.java.programmer


csiph-web