This discussion is archived
7 Replies Latest reply: Aug 5, 2009 9:32 PM by 793415 RSS

Addition and subtraction within a string

843789 Newbie
Currently Being Moderated
If I have a string such as "5-8+111"

What would be a reasonably simple way of computing the result of that. I have an array of these strings in a program and the porgram produces the above string...

Other examples include:

"10-5-8"

There are never any spaces or other characters. Just integers and - or + ...
  • 1. Re: Addition and subtraction within a string
    843789 Newbie
    Currently Being Moderated
    One way to do this is by using a ScriptEngine. See [reply #2 here|http://forums.sun.com/thread.jspa?messageID=9500729#9500729], courtesy of prometheuzz.

    For other examples, search for "ScriptEngine" on the forum. There are plenty.
  • 2. Re: Addition and subtraction within a string
    843789 Newbie
    Currently Being Moderated
    If that doesn't work you can use String.split(regex) to seperate the data into an array of strings. Then you have an array of numbers and operators which would be much easier to deal with. I haven't gotten a chance to use ScriptEngine so I'm not sure how effective it is.
  • 3. Re: Addition and subtraction within a string
    843789 Newbie
    Currently Being Moderated
    Thats the way I was thinking of doing it, however split() will split into an array of the terms, without operators. It can be done this way, however I thought there would be an easier/shorter way.
  • 4. Re: Addition and subtraction within a string
    843789 Newbie
    Currently Being Moderated
    Did you try using the ScriptEngine from reply #1 that I posted? All you have to do is modify it a bit to do what you're specifically asking for. I think this will be easier than using split().

    e.g.
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    
    public class ScriptEngineTest {
    
        public static void main(String[] args) {
    
            try {
    
                ScriptEngineManager manager = new ScriptEngineManager();
                ScriptEngine engine = manager.getEngineByName("JavaScript");
    
                String math = "5-8+111";
                Double result = (Double) engine.eval( math );
                System.out.println( math + " = " + result.toString() );
    
            } catch (ScriptException e) {
    
                e.printStackTrace();
    
            }
    
        }
    
    }
  • 5. Re: Addition and subtraction within a string
    793415 Pro
    Currently Being Moderated
    ibanezplayer85 wrote:
    ..e.g. ..
    Good example. Here is another [example of using the ScriptEngine|http://pscode.org/jse.html?eval=10-5-8].
  • 6. Re: Addition and subtraction within a string
    843789 Newbie
    Currently Being Moderated
    AndrewThompson may i ask if your job is a University Lecturer? You have the same name as one at my University
  • 7. Re: Addition and subtraction within a string
    793415 Pro
    Currently Being Moderated
    compSciUndergrad wrote:
    AndrewThompson may i ask if your job is a University Lecturer? ..
    Apparently yes ( you are capable of asking ;).

    [ Dang!  Just realised my pun was blunted by the fact that word was 'may' not 'can'. Oh well. ]
    ..You have the same name as one at my University
    I am not him.

    Do you mean the microbiologist from the UK?

    I once went on a search of people with the same name (the things we do when bored). My favourite namesake was a politician from Canada. The reports I found were other government ministers issuing a warning via the news media that if he "did not show his face in parliament within 30 days, he would be expelled". What a rebel.

    Edited by: AndrewThompson64 on Aug 6, 2009 2:30 PM