Java error: void cannot be converted to String
Java error: void cannot be converted to String
我知道有与此类似的帖子,但我似乎无法弄清楚我的问题。我觉得它有点小,但我的方法确实 return 一个字符串,所以我不确定为什么它告诉我类型为 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
您的函数没有 return 值 void
并且您尝试将其分配给字符串变量,这就是错误消息的含义。
所以改变:
public static void encrypt(String original) throws Exception {
到
public static String encrypt(String original) throws Exception {
和
return sb.toString()
所以 class 看起来像:
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();
}
}
顺便说一句:注意 Java 的命名约定。 Class 名字应该是主题。
编辑:
因为您的加密消息抛出异常,所以您也必须在 public static void main(String []args) throws Exception{
中抛出异常,或者您必须在 try-catch-block
中处理它
在您的代码中,您没有 return 为 MD5Digest class 中的加密方法执行任何操作,如下所示:
public static void encrypt(String original) throws Exception
这就是编译器给出错误的原因,因为它是 void 类型,但代码是 String
看起来你想要 return 一个字符串,它应该如下所示:
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();
}
}
我知道有与此类似的帖子,但我似乎无法弄清楚我的问题。我觉得它有点小,但我的方法确实 return 一个字符串,所以我不确定为什么它告诉我类型为 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
您的函数没有 return 值 void
并且您尝试将其分配给字符串变量,这就是错误消息的含义。
所以改变:
public static void encrypt(String original) throws Exception {
到
public static String encrypt(String original) throws Exception {
和
return sb.toString()
所以 class 看起来像:
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();
}
}
顺便说一句:注意 Java 的命名约定。 Class 名字应该是主题。
编辑:
因为您的加密消息抛出异常,所以您也必须在 public static void main(String []args) throws Exception{
中抛出异常,或者您必须在 try-catch-block
在您的代码中,您没有 return 为 MD5Digest class 中的加密方法执行任何操作,如下所示:
public static void encrypt(String original) throws Exception
这就是编译器给出错误的原因,因为它是 void 类型,但代码是 String
看起来你想要 return 一个字符串,它应该如下所示:
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();
}
}