is there a function for:

ex1:
String = "2+2.3/34"
output: double = 34.0

ex2:
String = "45/4+3-0.4"
output: double = 0.4

so, find the last operator and take the next number into double.
note there are no spaces.

Edited 3 Years Ago by game06

a function.. no, Java has methods. not sure if there's a method for this anywhere, but that doesn't mean you can't write one.
EDIT: I'm curious, though ... how do you get to that 'output'?
afaik: the output for the first would be somwhere around 2.18, and for the second 13.85, so how did you calculate yours?

Edited 3 Years Ago by stultuske

why should there be a ready made method for something that specific, that limited in scope?
It's not too hard to write your own using some regular expressions and a bit of knowledge of the core Java APIs.

And no, I'm not going to do that for you.

Not that I know of, but it's easy enough to loop backwards thru the string until you find an operator then take the substring from that place to the end. A minus sign is interesting - is it an operator or part of the number, as in "2*-3" ?

Edited 3 Years Ago by JamesCherrill

Hello,

such stuff is also simply possible in Java (you do not need a Java-written formula interpreter).

Since JDK 1.6 a build-in Javascript engine is standard.
(Check Oracle doc: Click Here)

Here is an example on how to use build-in Javascript from within a Java program:

/* 
  How to evaluate a formula using build-in Javascript?
    By courtesy of JDK 1.6 :)
*/

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class BuildInJavascript{
    public static void main (String args[]) {
      try {
         ScriptEngineManager sem = new ScriptEngineManager();
         ScriptEngine sen = sem.getEngineByName("JavaScript");
         String formula = "45/4 + 3 - 0.4";
         System.out.println("Evaluation of " + formula + " is " + sen.eval(formula));
         // Output: Evaluation of 45/4 + 3 - 0.4 is 13.85
      }
      catch (Exception e) {
        System.out.println("Exception: Wrong formula!");
      } 
   }
}

On a terminal you may compile this Java program:
javac BuildInJavascript.java

and run it:
java BuildInJavascript

Output is:
Evaluation of 45/4 + 3 - 0.4 is 13.85

Try it!

-- 1stDAN

Edited 3 Years Ago by 1stDAN

1stDan: do remember the point is to help them learn how to program, not to hand out custom made (and ready for copy-paste) code :)

Sorry stultuske, I will be trying to stick to this rule onwards, I promise!
For all that he has many postings, I don't believe that he will be able to program a formula interpreter himself. So I thought powerful build-in Javascript would quickly solve his problem (kind of breaking a butterfly on a wheel).

Edited 3 Years Ago by 1stDAN

I agree that using JavaScript like that is a very good way to evaluate arbitrary expressions at runtime in Java. I also agree that pointing the OP in the right direction is a more powerful learning tool than providing completed code.
On the other hand a recursive evaluator is only around 100 lines of code and could be an ideal learning exercise for someone at game06's level - to be sure (s)he will find it challenging, but that's what it takes to raise your game.

This article has been dead for over six months. Start a new discussion instead.