William Chang William Chang - 1 year ago 112
Java Question

Generating a random UUID and getting the version

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..

private UUID _uuid;
private String _uuidString;
private int _uuidVersion;

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();

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download