0

I just create unique id but with random number. but I want to create that id like jason-001,smith-002,etc.

Integer user_id= new Random().nextInt();

//implementation
reference_uploaded_by.getRef().setValue(name.getText().toString() + "-" + user_id);
Mr MD
  • 31
  • 5

1 Answers1

2

You can use AtomicLong#incrementAndGet as a counter. The “Atomic” means the class is thread-safe.

AtomicLong userIdIncrementor = new AtomicLong(previouslyUsedNumber);

Use that object to get incrementing numbers for each new user.

long number = userIdIncrementor.incrementAndGet();
String userId = userName + String.format("%03d", number);

This code assumes you will never have more than a thousand users, with only three digits for the number as you specified in your Question.

For details on formatting an integer, see this Answer.

Miha_x64
  • 5,973
  • 1
  • 41
  • 63