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 3 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 2 of 2 — ← Prev page 1 [2]


#19393

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-10-16 09:39 -0400
Message-ID<nospam-493397.09390616102012@news.aioe.org>
In reply to#19377
In article <3PZes.19616$zI3.16882@newsfe18.iad>,
 Daniel Pitts <newsgroup.nospam@virtualinfinity.net> wrote:

> 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.

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>

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


#19389

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-10-15 20:47 -0400
Message-ID<k5iar4$s7i$1@dont-email.me>
In reply to#19332
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
esosman@comcast-dot-net.invalid

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


#19417

FromArne Vajhoej <arne@vajhoej.dk>
Date2012-10-17 16:54 -0400
Message-ID<507f1b21$0$293$14726298@news.sunsite.dk>
In reply to#19332
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


[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web