Search code examples
javacompiler-errorsincompatibletypeerror

Java error: void cannot be converted to String


I know there are posts similar to this one, but I cannot seem to figure out my issue. I feel like it is something small, but my method does return a String, so I am no sure why it is telling me type void. I am new to java and would appreciate any help I can get. Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.security.MessageDigest;

public class Login{
    public static void main(String []args){
        Scanner user_input = new Scanner(System.in);

        String entered_username = Get_Credentials.get_username(user_input);
        String entered_password = Get_Credentials.get_password(user_input);
        String encrypted_password = MD5Digest.encrypt(entered_password);
        user_input.close();
        User[] all_users = File_Scanner.create_users();
    }
}

class Get_Credentials{
    public static String get_username(Scanner user_input){
        System.out.println("Username: ");
        return user_input.next();
    }

    public static String get_password(Scanner user_input){
        System.out.println("Password: ");
        return user_input.next();
    }

}

class File_Scanner{
    public static User[] create_users(){
        User users[] = new User[6];
        int index_counter = 0;

        try {
            File credentials_file = new File("credentials.txt");
            String pattern = "[^\"\\s]+|\"(\\\\.|[^\\\\\"])*\"";
            Scanner file_reader = new Scanner(credentials_file);

            while (file_reader.hasNextLine()) {
                users[index_counter] = new User();
                users[index_counter].username = file_reader.findInLine(pattern);
                users[index_counter].encrypted_password = file_reader.findInLine(pattern);
                users[index_counter].password = file_reader.findInLine(pattern);
                users[index_counter].role = file_reader.findInLine(pattern);
                file_reader.nextLine();
                index_counter++;
            }

            file_reader.close();
        }
        catch (Exception e) {
          System.out.println(e.getClass());
        }
        return users;
    }
}

class User {
    String username;
    String password;
    String encrypted_password;
    String role;
}

class MD5Digest {
    public static void encrypt(String original) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(original.getBytes());
        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());
    }

}

Here is the error I am getting:

Login.java:15: error: incompatible types: void cannot be converted to String
        encrypted_password = MD5Digest.encrypt(entered_password);
                                              ^
1 error

Solution

  • 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 {
    

    to

    public static String encrypt(String original) throws Exception {
    

    and

    return sb.toString()
    

    So the class looks like:

    class MD5Digest {
        public static String encrypt(String original) throws Exception {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(original.getBytes());
            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.

    EDIT:

    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