String result = "";
try{
int value = Integer.parseInt(a)+Integer.parseInt(b);
result = ""+value;
}catch(NumberFormatException ex){
//either a or b is not a number
result = "Invalid input";
}
MyStringSum.show(null,a+b);
I am using Wrapper Class but i want to sum two String values without using
Wrapper Class , can i do this?
You should not worry about the efficiency of your implementation, because it is not using any wrapper objects.
java.lang.Integer
plays several roles:
MIN_VALUE
and MAX_VALUE
, int
s by providing class methods for parsing and manipulating them, and Object
wrappers for primitive int
s, and providing instance methods for it. Because you use only parse(...)
, which is a class method , your code uses the class in its capacity #2, ignoring the #1 and #3. In other words, you are not using Integer
in its "wrapper for int
" capacity.
Try exp4j . Example(from the link):
Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)")
.variables("x", "y")
.build()
.setVariable("x", 2.3)
.setVariable("y", 3.14);
double result = e.evaluate();
Another option is Javaluator . See the link for example.
And there's EvalX . See below example(from the link):
Expression expression = new Expression("1+1/3");
result = expression.eval():
expression.setPrecision(2);
result = expression.eval():
I hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.