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 8 of 8

Thread: how to make Unique ID for regestered members?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question how to make Unique ID for regestered members?

    Hi,

    I am developing a JAVA code to generate UID for registered members which contains two digits related to year of register and 6 random integers.

    I have this code but it only generates three UID's I need to generate one UID when ever this code is run, and how to validate that this UID is really unique and not repeated?

    import java.util.Date;
    import java.util.HashMap;
    public class StudentsID 
    {
        private Date regDate;
        private static int idCount;
        private static HashMap<String,Integer> student_ids;
        public StudentsID()
        {
            regDate=new Date();
            student_ids=new HashMap<String,Integer>();
     
            idCount++;
     
     
        }
        public int makeID()
        {
            int step4=0;
     
            String step2;
            String step3;
            if(regDate.getYear()>99)
            {
                String step1=String.valueOf(regDate.getYear());
                step2=step1.substring(1);
            }
            else
                step2=String.valueOf(regDate.getYear());
     
            if(idCount<1000000)
            {
            step3=step2+0+0+0+0+0+0;
            step4= Integer.parseInt(step3)+idCount;
            }
            return step4;
        }
     
        public static void main(String...args)
        {
            StudentsID test1=new StudentsID();
            System.out.println(test1.makeID());
     
            StudentsID test2=new StudentsID();
            System.out.println(test2.makeID());
     
            StudentsID test3=new StudentsID();
            System.out.println(test3.makeID());
     
        }
     
    }
    Last edited by RedElvaan; May 21st, 2014 at 07:22 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to make Unique ID for regestered members?

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    Why do the 6 integers have to be random? It would be easiest if they started at 000001 and then counted up with each new member. Creating the six digits randomly and checking for uniqueness each time a member is added is more trouble than I think it's worth.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    RedElvaan (May 21st, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to make Unique ID for regestered members?

    Hi Greg,

    I think what you saying is right starting from 000001 as serial is better to reduce the validation process and to save time, but the thing is I need to include/attach to these digits two more digits from the left which is related to the year of registration

    ex.

    date of register : 21.05.2014 so java will take 14 from the year 2014 and add it to the serial number 000001 to be 14000001. This way the system will have up to 999999 available for members registered in 2014 and so on for next year if you get my point?!

  5. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: how to make Unique ID for regestered members?

    So, what do you have problems with? What is your question?

  6. #5
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to make Unique ID for regestered members?

    ops sry :p

    Ok could you give code examples to generate serial numbers?
    Last edited by RedElvaan; May 21st, 2014 at 07:26 AM.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to make Unique ID for regestered members?

    I won't give you code, but I suggest you check out the following topics:

    class variables or fields
    StringBuilder class

    and consider how those could be used to accomplish what you're trying to do.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    RedElvaan (May 21st, 2014)

  9. #7
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to make Unique ID for regestered members?

    Thanks for your kind help

  10. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: how to make Unique ID for regestered members?

    You could also do it without strings. You only need very basic math for this.

    If you want to shift a number to the left by a certain number of digits you just multiply with a power of 10.
    For example: 14 * 10^6 == 14 000 000

    And if you want the last 2 digits of a number you can do modulo calculation with a power of 10.
    For example: 2014 % 10^2 == 14

    So if you are given the year and a number of 6 random digits you can simply do:
    (year % 10^2) * 10^6 + rndNum

    For year == 2014 and rndNum == 123456 the result would be:
    (2014 % 10^2) * 10^6 + 123456
    == (2014 % 100) * 1000000 + 123456
    == 14 * 1000000 + 123456
    == 14000000 + 123456
    == 14123456

    which is what you want.

Similar Threads

  1. How to make a COMPLETELY UNIQUE Java GUI?
    By Tea.EarlGrey.Hot. in forum Java Theory & Questions
    Replies: 3
    Last Post: April 6th, 2014, 04:15 AM
  2. Storing unique random numbers
    By ajw1993 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 10th, 2013, 09:03 AM
  3. What unique/innovative game ideas did you like?
    By keysle in forum Totally Off Topic
    Replies: 0
    Last Post: May 25th, 2012, 10:56 PM
  4. unique identifier
    By jack_nutt in forum Java Theory & Questions
    Replies: 25
    Last Post: June 20th, 2011, 06:27 AM

Tags for this Thread