gpt4 book ai didi

java - 电子邮件方法不起作用

转载 作者:行者123 更新时间:2023-12-02 07:41:36 25 4
gpt4 key购买 nike

嘿,我有一个 EmployeeStore,我为此使用了 HashMap 。 map 存储的变量是电子邮件名称和 ID。我有一个名为 SearchByEmail 的方法,但有一个问题。当用户在 UI 中输入正确的员工电子邮件时,该方法返回 false。

这是我的代码:这是在主应用程序中

 case 2:
System.out.println("Search by Email.");
Employee employeeSearchEmail = MenuMethods.userInputByEmail();
Store.searchByEmail(employeeSearchEmail.getEmployeeEmail());

菜单方法

//Imports
import java.util.Scanner;
//********************************************************************

public class MenuMethods
{
private static Scanner keyboard = new Scanner(System.in);



//Methods for the Company Application menu.
//Method for validating the choice.
public static int getMenuChoice(String menuString, int limit, String prompt, String errorMessage)
{
System.out.println(menuString);
int choice = inputAndValidateInt(1, limit, prompt, errorMessage);
return choice;
}
//********************************************************************
//This method is used in the getMenuChoice method.
public static int inputAndValidateInt(int min, int max, String prompt, String errorMessage)
{
int number;
boolean valid;
do {
System.out.print(prompt);
number = keyboard.nextInt();
valid = number <= max && number >= min;
if (!valid) {
System.out.println(errorMessage);
}
} while (!valid);
return number;
}
//********************************************************************
public static Employee userInput()
{
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee ID:");
int employeeId = keyboard.nextInt();
temp = keyboard.nextLine();
System.out.println("Please enter the Employee E-mail address:");
String employeeEmail = keyboard.nextLine();
return e = new Employee(employeeName , employeeId, employeeEmail);

}
//********************************************************************
public static Employee userInputByName()
{
//String temp is for some reason needed. If it is not included
//The code will not execute properly.
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();

return e = new Employee(employeeName);

}
//********************************************************************
public static Employee userInputByEmail()
{
//String temp is for some reason needed. If it is not included
//The code will not execute properly.
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Email:");
String employeeEmail = keyboard.nextLine();
//This can use the employeeName's constructor because java accepts the parameters instead
//of the name's.
return e = new Employee(employeeEmail);

}
//********************************************************************


}

通过电子邮件搜索

public boolean searchByEmail(String employeeEmail) 
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
boolean employee = map.equals(employeeEmail);
System.out.println(employee);
return employee;

}

最佳答案

首先,

map.equals(employeeEmail);

确实没有意义。 map 是一个 HashmapemployeeEmail 是一个 String。在什么条件下它们会相等?

不清楚您在映射中存储什么以及如何存储,因为您既没有包含映射的声明,也没有包含插入新值的代码。我现在假设您存储像 name -> Employee 这样的映射。如果您想根据电子邮件地址搜索员工,我建议您执行类似的操作

Employee findByEmail(String email) {
for (Employee employee : yourMap.values())
if (employee.getEmail().equals(email))
return employee;

// Not found.
return null;
}

然后检查是否存在具有电子邮件的员工,您可以这样做

public boolean searchByEmail(String employeeEmail) {
boolean employee = findByEmail(employeeEmail) != null;
System.out.println(employee);
return employee;
}

关于java - 电子邮件方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11521148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com