making a string out of integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Katie87
    New Member
    • Oct 2007
    • 9

    #1

    making a string out of integers

    hey, i need to make an id. the first 3 numbers are 202, the second 2 are randomly generated, and the last one is an integer, which we get from somewhere else. the problem is the final value has to be stored in a long but i cant get the numbers together because the program just adds the integer values, instead of concatenating.
    does anyone know how i can do this.

    heres an example:

    int x= a random number(say 2)
    int y = another random number(say 3)
    int z= 1
    long id=202+ x+y+z;

    except i dont want them added i want
    long id= 202231
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Then you don't want to get them in a long with simple adding. There are two options I see:

    1) Use a string, adding the string equivalent of each number (you can use Integer.toStrin g(int) for this purpose) until you're done. Then use Long.parseLong( String) to get the long value of your string, which you can store in a Long variable.

    2) Find out how many digits each number has, then multiply the first number by 10^(digits of second + digits of third + ...), the second by 10^(digits of third + digits of fourth + ...), the third by 10^(digits of fourth + digits of fifth + ...), ... until the end. Then add all the numbers together.

    Comment

    • Katie87
      New Member
      • Oct 2007
      • 9

      #3
      Originally posted by Ganon11
      Then you don't want to get them in a long with simple adding. There are two options I see:

      1) Use a string, adding the string equivalent of each number (you can use Integer.toStrin g(int) for this purpose) until you're done. Then use Long.parseLong( String) to get the long value of your string, which you can store in a Long variable.

      2) Find out how many digits each number has, then multiply the first number by 10^(digits of second + digits of third + ...), the second by 10^(digits of third + digits of fourth + ...), the third by 10^(digits of fourth + digits of fifth + ...), ... until the end. Then add all the numbers together.


      great thanks alot that was perfect

      Comment

      Working...