How can I find the sum of two numbers which are in String variables?

In this code fragment, I can't sum a and b:

String a = "10"; String b = "20"; JOptionPane.showMessageDialog(null,a+b); 

Since a and b are defined as String, this code will concatenate the strings and output 10+20=1020.

How can I get it to instead sum a and b and output 10+20=30?

0

10 Answers

Java provides parse methods for Primitive Types. So depending on your input you can use Integer.parseInt, Double.parseDouble or others.

String result; try{ int value = Integer.parseInt(a)+Integer.parseInt(b); result = String. valueOf(value) ; }catch(NumberFormatException ex){ //either a or b is not a number result = "Invalid input"; } JOptionPane.showMessageDialog(null,result); 
0

Because you want to concat Strings they won't add up. You have to parse them to an Integer which works like:

Integer.parseInt(a) + Integer.parseInt(b) 

To sum this up + concats Strings and doesn't add them up.

use BigInteger class to perform largely in length string addition operation.

BigInteger big = new BigInteger("77777777777777777777888888888888888888888888888856666666666666666666666666666666"); BigInteger big1 = new BigInteger("99999999999999995455555555555555556"); BigInteger big3 = big.add(big1); 

try: Integer.parseInt(a)+Integer.parseInt(b)

 String a= txtnum1.getText(); String b= txtnum2.getText(); JOptionPane.showMessageDialog(null,Integer.parseInt(a)+Integer.parseInt(b)); 
2
public void actionPerformed(ActionEvent arg0) { String a= txtnum1.getText(); String b= txtnum2.getText(); String result = ""; try{ int value = Integer.parseInt(a)+Integer.parseInt(b); result = ""+value; }catch(NumberFormatException ex){ result = "Invalid input"; } JOptionPane.showMessageDialog(null,result); } 

it is work

Integer wrapper class has constructor which takes String parameter representing numbers.

String a= txtnum1.getText();//a="100" String b= txtnum2.getText();//b="200" Integer result; int result_1; String result_2; try{ result = new Integer(a) + new Integer(b); // here variables a and b are Strings representing numbers. If not numbers, then new Integer(String) will throw number format exception. int result_1=result.intValue();//convert to primitive datatype int if required. 

result_2 = ""+result; //or result_2 = ""+result_1; both will work to convert in String format

}catch(NumberFormatException ex){ //if either a or b are Strings not representing numbers result_2 = "Invalid input"; } 

We can change string to BigInteger and then sum its values.

import java.util.*; import java.math.*; class stack { public static void main(String args[]) { Scanner s=new Scanner(System.in); String aa=s.next(); String bb=s.next(); BigInteger a=new BigInteger(aa); BigInteger b=new BigInteger(bb); System.out.println(a.add(b)); } } 

Since + is used to concat strings, you can't use it to add two strings containing number. But subtraction works perfectly fine with such strings. Hence, you can use this basic mathematical concept to make that possible:

a-(-b) 

Integer.parseInt() is used to convert String to Integer.In order to perform sum of two strings first strings need to be converted to Integers and then need to perform sum otherwise it just concatenates two strings instead of performing sum.

String a = "10"; String b = "20";

JOptionPane.showMessageDialog(null,Integer.parseInt(a)+Integer.parseInt(b));

public String addTwoNumbers(final String n1, final String n2) { StringBuilder sb = new StringBuilder(); int carry =0; byte[] nb1; byte[] nb2; if (n1.length() > n2.length()){ nb1 = n1.getBytes(); nb2 = n2.getBytes(); } else { nb2 = n1.getBytes(); nb1 = n2.getBytes(); } int maxLen=n1.length()>=n2.length()?n1.length():n2.length(); for (int i = 1; i <= maxLen ; i++) { int a = nb1.length-i >= 0 ? nb1[nb1.length-i] - 48 : 0; int b = nb2.length-i >= 0 ? nb2[nb2.length-i] - 48 : 0; int result = a + b + carry; if (result >= 10){ carry = 1; result = result-10; } else { carry = 0; } sb.insert(0, result); } if(carry>0){ sb.insert(0, carry); } return sb.toString(); } 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like