Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a random UUID and getting the version

Tags:

java

uuid

I am trying to generate a UUID to a string, so that when I enter uuidcreaterandom, it will create a random UUID. I have pasted below what I tried, and am I missing something ? Any pointers much appreciated. Thank you..

public UUID uuidCreateRandom() {
    return UUID.randomUUID();
}

public UUID uuidCreateFromHexString(String uuid) {
    return UUID.fromString(uuid);
}

public String uuidCreateRandom(UUID uuid) {
    return uuid.toString();
}

public int uuidGetVersion(UUID uuid) {
    return uuid.version();
}

public UUID get_uuid() {
    return _uuid;
}

public String get_uuidString() {
    return _uuidString;
}

public int get_uuidVersion() {
    return _uuidVersion;
}

public void set_uuid() {
    _uuid = UUID.randomUUID();
}

public void set_uuidString(UUID uuid) {
    _uuidString = uuid.toString();
}

public void set_uuidVersion(UUID uuid) {
    _uuidVersion = uuid.version();
}
like image 274
William Chang Avatar asked Nov 03 '16 20:11

William Chang


1 Answers

It's not super clear what you're asking but the following code will generate a random UUID and get the string representation and version:

Scanner scanner = new Scanner(System.in);
while (true) {
   String line = scanner.nextLine().trim();
   if (line.equals("uuidcreaterandom")) {
      UUID uuid = UUID.randomUUID();
      String str = uuid.toString();
      System.out.println(str);
   }
}
like image 71
jenglert Avatar answered Sep 19 '22 14:09

jenglert