[英]Can i sum two String values without using Wrapper Class in java if yes than how ?
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类,但我想在不使用两个值的情况下求和
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: java.lang.Integer
扮演多个角色:
MIN_VALUE
and MAX_VALUE
, MIN_VALUE
和MAX_VALUE
, int
s by providing class methods for parsing and manipulating them, and int
的类方法 ,它可以作为使用原始int
的帮助类。 Object
wrappers for primitive int
s, and providing instance methods for it. int
的Object
包装,并为其提供实例方法 。 Because you use only parse(...)
, which is a class method , your code uses the class in its capacity #2, ignoring the #1 and #3. 因为仅使用
parse(...)
(这是一个类方法) ,所以您的代码以#2的身份使用该类,而忽略了#1和#3。 In other words, you are not using Integer
in its "wrapper for int
" capacity. 换句话说,您没有在
Integer
的“ wrapper for int
”功能中使用Integer
。
Try exp4j . 尝试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 . 另一个选择是Javaluator 。 See the link for example.
例如,请参阅链接。
And there's EvalX . 还有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! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.