Created
September 3, 2014 17:49
-
-
Save darkrishabh/1b6fd33e1b576e26dc9b to your computer and use it in GitHub Desktop.
Add two number in String format in JAVA - using Script Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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