# Thread: How to concatenate a number

1. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## 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)";  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Integer quantities are integer quantities and strings are strings. So that rules out the last three attempts:

Java 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")

Java 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.  Reply With Quote

3. Member Join Date
Mar 2011
Location
Bulacan, Philippines
Posts
23
Rep Power
0

## int n = (int)Math.floor(Math.random()*1000+1);
String s=n +""+n;
int a = Integer.parseInt(s);  Reply With Quote

4. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## 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.  Reply With Quote

5. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•