Operation in String to Double conversion

Discussion in 'Java' started by William Lopes, Oct 14, 2012.

  1. 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, Oct 14, 2012
    #1
    1. Advertising

  2. 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.
     
    William Lopes, Oct 14, 2012
    #2
    1. Advertising

  3. William Lopes

    markspace Guest

    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.
     
    markspace, Oct 14, 2012
    #3
  4. 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
     
    William Lopes, Oct 15, 2012
    #4
  5. William Lopes

    Roedy Green Guest

    On Sun, 14 Oct 2012 12:39:18 -0700 (PDT), William Lopes
    <> 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.
     
    Roedy Green, Oct 15, 2012
    #5
  6. In article <>,
    William Lopes <> 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>
     
    John B. Matthews, Oct 15, 2012
    #6
  7. William Lopes

    Guest

    > 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.
     
    , Oct 15, 2012
    #7
  8. William Lopes

    Guest

    > 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.
     
    , Oct 15, 2012
    #8
  9. William Lopes

    Lew Guest

    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
     
    Lew, Oct 15, 2012
    #9
  10. William Lopes

    Daniel Pitts Guest

    On 10/15/12 10:19 AM, John B. Matthews wrote:
    > In article <>,
    > William Lopes <> 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).
     
    Daniel Pitts, Oct 15, 2012
    #10
  11. William Lopes

    Guest

    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.
     
    , Oct 15, 2012
    #11
  12. William Lopes

    Lew Guest

    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
     
    Lew, Oct 15, 2012
    #12
  13. On 10/15/2012 06:24 PM, 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
     
    Arved Sandstrom, Oct 15, 2012
    #13
  14. On 15/10/2012 20:18, 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.
     
    Daniele Futtorovic, Oct 15, 2012
    #14
  15. William Lopes

    Guest

    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.
     
    , Oct 15, 2012
    #15
  16. William Lopes

    Eric Sosman Guest

    On 10/14/2012 3:39 PM, 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.


    I wrote an expression evaluator will accept such a string and
    produce an object instance that can then be evaluated:

    Expression e = Expression.compile("100 + 10", null);
    double value = e.value(null);

    More generally,

    // Formula for volume of a cylinder:
    Expression e = Expression.compile(
    "(diameter/2)^2 * PI * height",
    new String[] { "diameter", "height" });

    // Volume of a cylinder of diameter 4, height 10:
    double volume = e.value(new double[] { 4.0, 10.0 });

    The only unusual feature is that expressions compile directly to
    Java byte code rather than to an interpreted pseudo-code; their
    value() methods can therefore be JITted for speedier execution.
    This is surely overkill for your problem (and for many others;
    it was an over-engineered learning exercise), but should solve it
    nonetheless. Internally, though, the compiler splits the string
    "of my way manually," which you seem to find distasteful. Sue me.

    Send an E-mail address if you'd like a copy. No warranties.

    --
    Eric Sosman
    d
     
    Eric Sosman, Oct 16, 2012
    #16
  17. In article <3PZes.19616$>,
    Daniel Pitts <> wrote:

    > On 10/15/12 10:19 AM, John B. Matthews wrote:
    > > In article <>,
    > > William Lopes <> 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.


    Cited for a tantalizing suggestion regarding TableModel and an important
    tip on pronunciation.

    <http://commons.apache.org/ognl/>

    > It *is* a programming language in its own rite, so use with caution
    > (eg, only strings you from trusted sources).


    This bears repeating; it also applies to the RhinoScriptEngine, et al.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Oct 16, 2012
    #17
  18. William Lopes

    Arne Vajhoej Guest

    On 10/14/2012 3:39 PM, William Lopes wrote:
    > 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.


    Many options:
    * Write an expression evaluator from scratch
    * Write an expression evaluator using a parser generator
    * Generate Java code, compile it, load it and call it
    * Call a script engine (JavaScript, BeanShell etc.)

    Unless performance is critical I would go for BeanShell. Any
    Java developer should know the syntax and it is relative easy
    to call.

    Arne
     
    Arne Vajhoej, Oct 17, 2012
    #18
  19. William Lopes

    Arne Vajhoej Guest

    On 10/15/2012 6:14 PM, Arved Sandstrom wrote:
    > On 10/15/2012 06:24 PM, 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
     
    Arne Vajhoej, Oct 17, 2012
    #19
  20. William Lopes

    Arne Vajhoej Guest

    On 10/15/2012 5:24 PM, 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
     
    Arne Vajhoej, Oct 17, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,647
    Victor Bazarov
    Feb 17, 2005
  2. david ullua
    Replies:
    13
    Views:
    694
  3. J.M.
    Replies:
    5
    Views:
    809
  4. raan
    Replies:
    2
    Views:
    471
  5. Giuseppe:G:
    Replies:
    7
    Views:
    427
    Giuseppe:G:
    Jul 12, 2008
Loading...

Share This Page