Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:21:15+00:00 2026-05-14T04:21:15+00:00

I want to generate unique id’s everytime i call methode generateCustumerId(). The generated id

  • 0

I want to generate unique id’s everytime i call methode generateCustumerId(). The generated id must be 8 characters long or less than 8 characters.
This requirement is necessary because I need to store it in a data file and schema is determined to be 8 characters long for this id.

Option 1 works fine. Instead of option 1, I want to use UUID. The problem is that UUID generates an id which has to many characters. Does someone know how to generate a unique id which is less then 99999999?

option 1

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class CustomerIdGenerator {

private static Set<String> customerIds = new HashSet<String>();
private static Random random = new Random();

// XXX: replace with java.util.UUID
public static String generateCustumerId() {

    String customerId = null;
    while (customerId == null || customerIds.contains(customerId)) {
        customerId = String.valueOf(random.nextInt(89999999) + 10000000);
    }
    customerIds.add(customerId);
    return customerId;
}

}

option2 generates an unique id which is too long

 public static String generateCustumerId() {
    String ownerId = UUID.randomUUID().toString();
    System.out.println("ownerId " + ownerId);
    return ownerId
}
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:21:15+00:00Added an answer on May 14, 2026 at 4:21 am

    Does it need to be unique or random? If it only needs to be unique, you could load the highest value from the datastore when the application is launched (assuming only one app is writing to the data file). One you have your highest value:

    public class IdGenerator{
      private String value;
    
      public IdGenerator( String initial value ){
        this.value = value;
      }
    
      public synchronized String nextValue(){
        value = incrementValue( value );
        return value;
      }
    
      private static String nextValue( String currentValue ){
        // Somehow increment the value.
        return incrementedValue;
      }
    
    }
    

    Depending on what characters you allow in you uuid this can be done various ways. I would read the last character of the current String, check if it’s the last allowed character. Is so, increment the 7th character and so on. Else, increment the last character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.