# how to add strings as numbers?

• December 8th, 2013, 12:47 PM
Dixxon
how to add strings as numbers?
hey

i am trying to add two strings as numbers, as an exampel :

String numb1 ="30";
String numb2 ="30";

where the answer would be 60 and not 3030. any ideas how to do that ?
• December 8th, 2013, 12:49 PM
andbin
Re: how to add strings as numbers?
Quote:

Originally Posted by Dixxon
i am trying to add two strings as numbers, as an exampel :

String numb1 ="30";
String numb2 ="30";

where the answer would be 60 and not 3030. any ideas how to do that ?

Convert (better to say "parse") String to int. See parseInt, static method of java.lang.Integer.
• December 8th, 2013, 12:52 PM
Dixxon
Re: how to add strings as numbers?
will that work ? as the method i am using is a string method , looks like this
Code :

```public static String add (String numb1, String nub2) { // any code }```
• December 8th, 2013, 01:00 PM
andbin
Re: how to add strings as numbers?
Quote:

Originally Posted by Dixxon
will that work ?

How/where you encapsulate the sum of two string-numbers is not very much important. The important base concept is the conversion from String to int (or long or double or other primitive, you can choose). And I repeat: see the parseInt method of Integer.
• December 8th, 2013, 01:13 PM
Dixxon
Re: how to add strings as numbers?
i think it would look like :
Code :

``` public static String addera (String tal1, String tal2) { String Numb1= "30"; String Numb2 ="30"; int IntNumb1 = Integer.parseInt(Numb1); int IntNumb2 = Integer.parseInt(Numb2); return(IntNumb1 + IntNumb2); // gives an error "Type mismatch: cannot convert from int to String" }```

PS: i am new to java so excuse my questions....
• December 8th, 2013, 01:40 PM
andbin
Re: how to add strings as numbers?
Quote:

Originally Posted by Dixxon
i think it would look like :
Code :

``` return(IntNumb1 + IntNumb2); // gives an error "Type mismatch: cannot convert from int to String"```

return "" + IntNumb1 + IntNumb2;

or (better)

return Integer.toString(IntNumb1 + IntNumb2);

or also (Integer.toString is called under the hood)

return String.valueOf(IntNumb1 + IntNumb2);
• December 8th, 2013, 01:56 PM
Dixxon
Re: how to add strings as numbers?
oh yeah thanks , i got it work with String.valueOf()

but while adding 3333333333333 + 3 it gives me an error :

Exception in thread "main" java.lang.NumberFormatException: For input string: "3333333333"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at OperationerMedNaturligaHeltalGivnaSomTeckenstranga r.main(OperationerMedNaturligaHeltalGivnaSomTecken strangar.java:19)

any ideas?
• December 8th, 2013, 03:55 PM
andbin
Re: how to add strings as numbers?
Quote:

Originally Posted by Dixxon
but while adding 3333333333333 + 3 it gives me an error :

Exception in thread "main" java.lang.NumberFormatException: For input string: "3333333333"

3333333333333 is more than the largest int value. An int is 32 bit, ranges from -2147483648 to +2147483647.

Quote:

Originally Posted by Dixxon
any ideas?

Use long (64 bit). And please, see Long.parseLong method.