简体   繁体   English

如果可以的话,我可以在不使用java中的Wrapper类的情况下求和两个String值吗?

[英]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扮演多个角色:

  1. It holds several important constants, such as MIN_VALUE and MAX_VALUE , 它包含几个重要的常量,例如MIN_VALUEMAX_VALUE
  2. It serves as a helper class for working with primitive int s by providing class methods for parsing and manipulating them, and 通过提供用于解析和操纵原始int类方法 ,它可以作为使用原始int的帮助类。
  3. Its instances serve as Object wrappers for primitive int s, and providing instance methods for it. 它的实例充当原始intObject包装,并为其提供实例方法

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.

相关问题 可以使用Java中的包装器类交换两个数字,而无需创建其他任何类吗? - Can two numbers be swapped using wrapper class in java without creating any other class? 如何使用 java 中的 useDelimeter 从字符串中获取两个特定值? - How can i get two specific values from a String using useDelimeter in java? SpringBoot:如何使用 Wrapper Class 将两个 JSON pojo 绑定到两个单独的对象中并将它们保存到我的数据库中? - SpringBoot:How can I bind two JSON pojo into two seperate objects using Wrapper Class and save them to my DB? 是否可以在java中获取class object的值,如果可以如何获取? - Is it possible to have values for class object in java, if yes how can we acquire it? 如何使用两个不同的值对Java列表进行排序? - How can I sort a Java list using two different values? 如何在不使用sum ++的情况下正确计算总和? - How can i calculate the sum correctly without using sum++;? 如何在 java 中不使用 sort() 方法对字符串进行排序? - How can i sort a string without using sort() method in java? 使用JAVA-如何在不使用数组的情况下计算输入字符串的总和及其长度? - Using JAVA - How can I compute the total sum of input strings & their lengths without using Array? 如何使用Java中的StringReader类读取字符串? - How can I read a String using StringReader class in Java? 如何在两个映射中求和并使用番石榴返回值 - how can I sum the values in two maps and return the value using guava
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM