Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Generate users id

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generate users id

    hello guys,

    I am doing my own project and i have reach a point that i need to generate users id with format 000000 up to 999999 without allowing any number to repeat its self. How can i do that with java?
    Any answer is appreciated.
    Thanks in advance.


  2. #2

    Default Re: Generate users id

    You should choose the location of where you post more carefully. This should be in the general questions area.

    Create a recursive function that generates a random number between 0 and 9 and then feeds that number to the next recursion, but specify that the particular number (x) that your received in the previous step cannot be the next number generated. If so, then generate the number again.

    Read up on recursion here: Recursion - Wikipedia, the free encyclopedia

    public String generateNumberRecusively(int numDigits)
    {
         return getNextNumberHelper("x", 6, 0);
    }
     
    public String getNextNumberHelper(String previousNumber, int numDigits, int digitCount)
    {
         if(previousNumber.equals("x")||)
         {} //first iteration, TO-DO... generate a number 0-9, then call self and increment counter
         else if(digitCount == numDigits) // generate a number 0-9 but not previousNumber then return previosNumber + generatedDigit
         {}
         else // generate a number 0-9 but not previousNumber then call self and increment counter
         {}
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Generate users id

    I have moved your post to a more appropriate forum.

    I'm not sure if kenster421's advice actually solves your problem - it seems it solves something else entirely.

    You can do this 2 (and probably many more) ways. First, just loop up from 0000000 onwards. Second, Create a Set and generate a random number between those specified - checking the Set to see if that number has already been generated - if not continue, if so generate another number. Try writing some code, and post back if you have more problems.

  4. #4

    Default Re: Generate users id

    Quote Originally Posted by copeg View Post
    I have moved your post to a more appropriate forum.

    I'm not sure if kenster421's advice actually solves your problem - it seems it solves something else entirely.

    You can do this 2 (and probably many more) ways. First, just loop up from 0000000 onwards. Second, Create a Set and generate a random number between those specified - checking the Set to see if that number has already been generated - if not continue, if so generate another number. Try writing some code, and post back if you have more problems.
    If you read his post, you would see that he needed to generate a six digit number with no repeats. The solution I tried to steer him to was to generate the number, digit by digit, making sure that the previous digit used will not be the next digit, i.e., no repeats. Not only does it do that, but you can specify what length you want the number to be. I am unsure as to what "other" problem my code solves.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Generate users id

    Quote Originally Posted by kenster421 View Post
    If you read his post, you would see that he needed to generate a six digit number with no repeats. The solution I tried to steer him to was to generate the number, digit by digit, making sure that the previous digit used will not be the next digit, i.e., no repeats. Not only does it do that, but you can specify what length you want the number to be. I am unsure as to what "other" problem my code solves.
    Then it is up to the original poster to clarity...because I read the initial problem as to generate user id's which are represented by 6 digits - and no user id can be a duplicate of another (as opposed to no digit within the user id can be duplicate)

  6. #6

    Default Re: Generate users id

    Quote Originally Posted by copeg View Post
    Then it is up to the original poster to clarity...because I read the initial problem as to generate user id's which are represented by 6 digits - and no user id can be a duplicate of another (as opposed to no digit within the user id can be duplicate)
    Too true. If it is the case that you interpreted it as, then a simple incremental counting variable would have sufficed.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. java and blind users
    By micam001 in forum Member Introductions
    Replies: 1
    Last Post: June 27th, 2011, 03:27 AM
  2. create some fake users on server
    By nakres in forum Paid Java Projects
    Replies: 1
    Last Post: March 25th, 2011, 10:37 AM
  3. How to Get users home directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: September 2nd, 2010, 10:37 AM
  4. How to Get users home directory
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 2nd, 2010, 10:37 AM
  5. How to know number of user downloading an application?
    By jazz2k8 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2008, 04:34 AM