Skip to content

Instantly share code, notes, and snippets.

@darkrishabh
Created September 3, 2014 17:49
Show Gist options
  • Save darkrishabh/1b6fd33e1b576e26dc9b to your computer and use it in GitHub Desktop.
Save darkrishabh/1b6fd33e1b576e26dc9b to your computer and use it in GitHub Desktop.
Add two number in String format in JAVA - using Script Engine
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import java.util.Scanner;
class Script{
public static void main(String args[])throws IOException
{
Scanner me = new Scanner(System.in);
System.out.println("Enter 2 numbers:");
String numOne = me.nextLine();
String numTwo = me.nextLine();
try{
ScriptEngine js = new ScriptEngineManager().getEngineByName("javascript");
String expression =numOne+"+"+numTwo;
System.out.println(js.eval(expression));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment