I have two integers, x and y. What I am trying to do, is combine the numbers in both, not add them together. I have tried to do this:
int x = 5;
int y = 10;
sum = x + y;
But that makes the output 15. What I am wondering is if there is any way to combine them, so that the output would be 510.
5 + 10 = 510
That is what I am trying to accomplice.
I know I could do something like this:
int x = 5;
int y = 10;
int sum;
sum = Convert.ToInt32(x.ToString() + y.ToString());
But that seems like a sloppy way to do it. Is there a better way to do this?
Thanks.
If you want to concatenate a string and a number, such as an integer int or a floating point float , convert the number to a string with str() and then use the + operator or += operator.
You need to convert the parameter inside the println method into a string literal ,then java compiler would recognize it as a string and will not add two integers.
To concatenate a String and some integer values, you need to use the + operator. Let's say the following is the string. String str = "Demo Text"; Now, we will concatenate integer values.
A little simplier:
int x = 5;
int y = 10;
int sum;
sum = Convert.ToInt32("" + x + y);
Notice that you need convertion in any case. Implicit conversion is used here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With