Java错误:void无法转换为String

时间:2019-02-21 17:52:53

标签: java compiler-errors incompatibletypeerror

我知道有类似的帖子,但是我似乎无法弄清楚我的问题。我觉得它很小,但是我的方法确实返回了String,所以我不确定为什么它告诉我键入void。我是Java新手,不胜感激。这是我的代码:

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

}

这是我遇到的错误:

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

2 个答案:

答案 0 :(得分:2)

您的函数没有返回值void,您尝试将其分配给字符串变量,这就是错误消息的含义。

所以改变:

public static void encrypt(String original) throws Exception {

public static String encrypt(String original) throws Exception {

return sb.toString()

所以类看起来像:

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:请注意Java的命名约定。班级名称应为主题。

编辑:

由于加密消息会引发异常,因此您还必须在public static void main(String []args) throws Exception{中引发异常,或者必须在try-catch-block中对其进行处理

答案 1 :(得分:0)

在您的代码中,您不会为MD5Digest类中的加密方法返回任何内容,如下所示:

public static void encrypt(String original) throws Exception

这就是为什么编译器给出错误的原因,因为它是空类型,但代码用于String

看起来您想返回一个String,它应该如下所示:

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

}