Pushpa
Pushpa

Reputation: 109

Converting randomly generated number to String

I had generated one random number using UUID.randomUUID().

I get that random number as

615166860114132342

Now I am trying to convert this number to String using, 615166860114132342.toString(). But I am getting error. Ultimately what I need is to get string format of that random number because I need to check some logs regarding that string. So , how to get it. Is there any way?

Thank you in advance.

Upvotes: 0

Views: 114

Answers (2)

Ankur Singhal
Ankur Singhal

Reputation: 26067

This work perfectly fine for me.

import java.util.UUID;

public class Test {
    public static void main(String[] args) {
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
    }
}

Output

766dedf1-a8cd-4572-a01f-c99b5cd45c50

On Pushpa request

public class Test {
    public static void main(String[] args) {
        String uuid = UUID.randomUUID().toString();
        long value = UUID.randomUUID().getMostSignificantBits();
        System.out.println(value);

        String val = String.valueOf(value);
        System.out.println(val);

        System.out.println(uuid);
    }
}

output

4421583623795395631
4421583623795395631
868b5b0f-92d8-44ca-89c1-d93e98187262

Upvotes: 2

the6p4c
the6p4c

Reputation: 664

To retrieve the String representation of a number, use the function String#valueOf(Object v).

Therefore: String.valueOf(UUID.randomUUID());

Upvotes: 0

Related Questions