Results 1 to 5 of 5
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 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)";

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

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

    Default

    int n = (int)Math.floor(Math.random()*1000+1);
    String s=n +""+n;
    int a = Integer.parseInt(s);
    Keep Moving Forward

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default


    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.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by nap_patague View Post
    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.

Similar Threads

  1. Concatenate year from DATETIME
    By ashin in forum SWT / JFace
    Replies: 11
    Last Post: 07-30-2010, 06:34 PM
  2. Concatenate Linked Lists
    By tttestall in forum New To Java
    Replies: 1
    Last Post: 04-20-2010, 08:03 PM
  3. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  4. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM
  5. Replies: 1
    Last Post: 11-03-2008, 03:38 PM

Posting Permissions

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