2

Is it possible to convert a String such as 1 + 2 into an integer that equals 3?

I know I can use valueOf() to get the value of individual numbers

String test1 = "1";
String test2 = "2";
int test3 = Integer.valueOf(test1);
int test4 = Integer.valueOf(test2);

int answer = test3 + test4;
System.out.println(answer);

but is it possible to convert "1 + 2" to 3 in one step instead of two?

Schmidty15
  • 181
  • 1
  • 2
  • 7

5 Answers5

4

Java comes packaged with Rhino.

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

public class RhinoExample {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine rhino = manager.getEngineByName("JavaScript");

        Double result = (Double) rhino.eval("1 + 2");
        Integer i = result.intValue();
        System.out.println(i);
    }
}
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • He has two strings, `"1"` and `"2"`. Does `rhino` handle this? Your example seems to suggest `rhino` expects a single string with math operators within the string. – nhgrif Nov 18 '13 at 19:00
  • 1
    @rhgrif Rhino is a fully functional JavaScript implementation. You can use [`put`](http://tinyurl.com/lf55j6t) to create variables and process it that way or you could do `val1 + " + " + val2` before invoking `eval`. – Jeffrey Nov 18 '13 at 19:02
  • @Jeffrey Now does this have to go in public static void main? Or is there another way to throw the ScriptException from another method? – Schmidty15 Nov 18 '13 at 19:42
  • @user1984712 You can put this in any method you wish. You can either make that method throw the `ScriptException` or you can use a [`try-catch` statement](http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html) to handle it. – Jeffrey Nov 19 '13 at 15:37
2

you can use javax.script.ScriptEngineManager and javax.script.ScriptEngine; Check this post Evaluating a math expression given in string form

Community
  • 1
  • 1
Lucas Oliveira
  • 833
  • 1
  • 10
  • 24
0

The standard java libraries does not allow you to do this. However as Jeff Storey answered here you may try like this:

ScriptEngineManager mg= new ScriptEngineManager();
ScriptEngine engine = mg.getEngineByName("js");        
Object result = engine.eval("1+2");

However if you dont want to use the above method the best you can do is this:

String test1 = "1";
String test2 = "2";
System.out.println((Integer.valueOf(test1)+Integer.valueOf(test2)));
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0
public void printStringSum(String s1, String s2) {
    int i1 = Integer.valueOf(s1);
    int i2 = Integer.valueOf(s2);
    val = i1 + i2;
    System.out.println(val);
}

Now where ever it is in the code that you're wanting to do what's posted in the original code, just call this method:

printStringSum("1", "2");

However, you can improve this further by making the method expect an array of Strings. Within the method body, iterate through the string, taking the value and +=-ing it to val, then printing val at the end.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

Unless it's necessary for memory reasons, Id stick with the way you have it, for debugging reasons especially if test1 and test2 are eventually some kind of user input.

String test1 = "1";
String test2 = "2";
System.out.println((Integer.valueOf(test1)+Integer.valueOf(test2)));

This works as well

String test1 = "1";
String test2 = "2";
int answer = (Integer.valueOf(test1)+Integer.valueOf(test2));
System.out.println(answer);

if its REALLY necessary to limit the number of lines and you can gaurentee that test1 and test2 are going to be "1" and "2" then just use:

System.out.println((Integer.valueOf("1")+Integer.valueOf("2")));
Snowdrama
  • 405
  • 1
  • 6
  • 17