Java error: void cannot be converted to String

Your function has no return value void and you try to assign it to a string variable, that is what the error message means.

So change:

public static void encrypt(String original) throws Exception {


public static String encrypt(String original) throws Exception {


So the class looks like:

class MD5Digest {
    public static String encrypt(String original) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));

        System.out.println("original:" + original);
        System.out.println("digested:" + sb.toString());
        return sb.toString();


BTW: Take care of Java’s naming convention. Class names should be subjects.


Because your encrypt message throws an exception, you have to throw the exception also in public static void main(String []args) throws Exception{ or you have to handle it in a try-catch-block

Leave a Comment