# How to concatenate a number

• 03-27-2011, 02:49 AM
jim01
How to concatenate a number
I am trying to figure out how to concatenate the same number. For instance, if a randomly generated the number is 621, how do you make it 621621? I can do it in the output but cannot figure out how to do it with a statement.

I've tried the following, none of which have worked:

int n = (int)Math.floor(Math.random()*1000+1);
String a = "n" + "n";

int n = (int)Math.floor(Math.random()*1000+1);
int a = "n" + "n";

int n = (int)Math.floor(Math.random()*1000+1);
int a = "n + n";

int n = (int)Math.floor(Math.random()*1000+1);
int a = "(n + n)";
• 03-27-2011, 03:11 AM
pbrockway2
Integer quantities are integer quantities and strings are strings. So that rules out the last three attempts:

Code:

`int a = "<anything here>";`

This code is not going to fly and your compiler should have told you this. (btw compiler messages are better to post if you don't understand them rather than saying that they "don't work")

Code:

`String s = "n" + "n";`

This has the merit of compiling, but if you concatenate two one character strings you are just going to get a two character string which is not what you are after.

-----------

The question needs to be made a bit more precise. Are you after a string result or an integer quantity result? If "(int)Math.floor(Math.random()*1000+1)" yielded 42 should the result be 4242 (or "4242") or 42042 (or "42042")?

You might want to think about using String.valueOf(42) and Integer.parseInt("42") to convert between integer quantities and strings.

Or just think about the value of n*1000+n if you prefer arithmetic to string manipulation. (The 1000 may be problematic depending on what exactly you are trying to do.)

--------------

Maybe jjth39347 could chime in if he's still online.
• 03-27-2011, 03:11 AM
nap_patague
int n = (int)Math.floor(Math.random()*1000+1);
String s=n +""+n;
int a = Integer.parseInt(s);
• 03-27-2011, 03:24 AM
jim01
Quote:

The question needs to be made a bit more precise. Are you after a string result or an integer quantity result? If "(int)Math.floor(Math.random()*1000+1)" yielded 42 should the result be 4242 (or "4242") or 42042 (or "42042")?

You might want to think about using String.valueOf(42) and Integer.parseInt("42") to convert between integer quantities and strings.

Or just think about the value of n*1000+n if you prefer arithmetic to string manipulation. (The 1000 may be problematic depending on what exactly you are trying to do.)

--------------

Maybe jjth39347 could chime in if he's still online.
Sorry for the poor description. What I am looking to do is take a randomly generated three digit number and put it together with itself. So if a 3 is generated, I need to put it together to form 003003. If it's a 871 then I need to put it together to form 871871.

I looked at the java docs but didn't know really where to look and the three Java books I have do not show how to concatenate numbers. Anyway, I'll keep trying to figure it out.
• 03-27-2011, 03:31 AM
jim01
Quote:

Originally Posted by nap_patague
int n = (int)Math.floor(Math.random()*1000+1);
String s=n +""+n;
int a = Integer.parseInt(s);

Ah, I didn't even think about using Integer.parseInt(s)! I was totally on the wrong track!

Thank you very much.