question about string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

question about string

I havetwo strings : Str1=3 Str2=8 I want to add two strings together: Str1+str2=11 Is this possible?

12th Apr 2023, 10:20 PM
3axpa
3axpa - avatar
4 Answers
+ 5
No, it is not possible to add the two strings "3" and "8" directly to get the mathematical sum of 11. In Java, when you use the "+" operator to concatenate strings, it simply combines the characters in the strings into a single new string. So if you concatenate "3" and "8" with the "+" operator like this String str1 = "3"; String str2 = "8"; String result = str1 + str2; The result string would be "38", which is the concatenation of the characters in both strings. To add the numeric values of the two strings, you first need to convert them into integers using the Integer.parseInt() method, and then perform the addition. Here's an example: String str1 = "3"; String str2 = "8"; int num1 = Integer.parseInt(str1); int num2 = Integer.parseInt(str2); int sum = num1 + num2;
13th Apr 2023, 7:20 AM
Sadaam Linux
Sadaam Linux - avatar
+ 7
// in Java String Str1 = "3"; String Str2 = "8"; int num1 = Integer.parseInt(Str1); int num2 = Integer.parseInt(Str2); System.out.print(num1 + num2);
12th Apr 2023, 10:40 PM
SoloProg
SoloProg - avatar
+ 1
Tnx :D
12th Apr 2023, 10:43 PM
3axpa
3axpa - avatar
+ 1
Thank you :)
13th Apr 2023, 12:32 PM
3axpa
3axpa - avatar