Results 1 to 3 of 3
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Auto id generation

    Hello,

    I have a program that reads the name of a person and it is required an ID to be allocated to it automatically by the system such as 1, 2, 3, 4, ...
    Could someone tell me how to write a code that first allocates an id of 1 to the first person when it is read and an id of 2 the second person when it is read and so on?
    Thanks in advance

  2. #2
    bezudar is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    make a static variable called id and increment it--
    e.g
    public static long x = 0;
    public static long getID(){
    return x++;
    }

  3. #3
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by bezudar View Post
    make a static variable called id and increment it--
    e.g
    public static long x = 0;
    public static long getID(){
    return x++;
    }
    This will actually start it at ID of 0.

    You could either change long x = 1;

    or

    return ++x;

Similar Threads

  1. War file generation
    By vislawath.jawaharlal in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-07-2009, 02:12 PM
  2. Auto-complete/Auto-fix for custom statement
    By dark_cybernetics in forum Eclipse
    Replies: 0
    Last Post: 08-19-2008, 12:19 PM
  3. EJB generation with xdoclet tags
    By gugas in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 02-18-2008, 02:07 PM
  4. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 04:43 AM
  5. String generation
    By codingisfun22 in forum Advanced Java
    Replies: 3
    Last Post: 12-03-2007, 06:43 AM

Posting Permissions

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