0

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

这是我的代码:这是在 MainApp

 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;

    }
4

2 回答 2

1

首先,

map.equals(employeeEmail);

真的没有意义。map是一个Hashmap,并且employeeEmail是一个String。在什么条件下它们会相等?

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

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

    // Not found.
    return null;
}

然后检查员工是否email存在,你可以做

public boolean searchByEmail(String employeeEmail) {
    boolean employee = findByEmail(employeeEmail) != null;
    System.out.println(employee);
    return employee;
}
于 2012-07-17T11:03:34.240 回答
1

我假设 map 是Map<S,T>某些 S,T 的类型,因此它与 的类型不同employeeEmail,特别是它不是equals()

我怀疑您正在寻找Map.containsValue()(如果电子邮件是地图中的值)或Map.containsKey()(如果电子邮件是地图的键),这取决于map映射的确切内容,如果映射是到/来自字符串值。

编辑:基于对评论的澄清:
由于电子邮件不是 中的键或值map,因此建议的解决方案将无法正常工作。因此,您可以选择其中之一:

  1. 使用@aioobe 的解决方案来迭代和检查每封电子邮件。
  2. 向类添加一个额外的字段:Map<String,Employee> map2它将映射:email_address->employee。鉴于此地图,您可以使用 搜索电子邮件map2.containsKey(email)。它将确保从电子邮件中更快地查找员工并扩大持有额外地图的范围。如果我是你,我会选择这个。
于 2012-07-17T11:04:27.877 回答