Question

I'm shortening my UUID into a four digit number using the following code:

import java.nio.ByteBuffer;
import java.util.UUID;

/**
 * Generate short UUID (13 characters)
 *
 * @return short UUID
 */

public class UUIDshortener
{
    public static String shortUUID()
{
    UUID uuid = UUID.randomUUID();
    //long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong();
    //return Long.toString(l, Character.MAX_RADIX);
    return uuid.toString().hashCode();
}
}

I am getting the following error: compiler error message

If anyone knows how to fix this it would be much appreciated. Is it as simple as changing it to an int?

Était-ce utile?

La solution

Your method signature says you will return a String

public static String shortUUID()

but you are actually returning an int, since that's the return type of the hashCode() method

return uuid.toString().hashCode();

Just change your method sig to:

public static int shortUUID()

EDIT - to answer comment

There is no getInt() method on the UUID class, so uuid.getInt().hashCode() won't compile. If you want to simplify your return statement, you can just hashCode() directly on uuid like this:

    return uuid.hashCode();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top