电子邮件方法不起作用

时间:2012-07-17 11:01:24

标签: java search hashmap

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

这是我的代码: 这是在MainApp

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

MenuMethods

//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);

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


}

SearchByEmail

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;

    }

2 个答案:

答案 0 :(得分:1)

首先,

map.equals(employeeEmail);

没有意义。 mapHashmapemployeeEmailString。在什么条件下它们是平等的?

目前还不清楚你在地图中存储了什么,以及如何既没有包含地图声明,也没有包含插入新值的代码。我现在假设您存储了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;
}

答案 1 :(得分:1)

我假设某些S,T的地图属于Map<S,T>类型,因此它与employeeEmail的类型不同,具体而言它不是equals()

我怀疑您正在寻找Map.containsValue()(如果电子邮件是地图中的值)或Map.containsKey()(如果电子邮件是地图的关键字),具体取决于{{1如果映射是来自/来自字符串值,则为映射。

编辑:根据对评论的澄清:
由于电子邮件不是map中的密钥或值,因此建议的解决方案不会按原样运行。所以你可以选择其中一个:

  1. 使用@ aioobe的解决方案迭代并检查每封电子邮件。
  2. 在课程中添加一个额外字段:map,其中将映射:Map<String,Employee> map2。根据此地图,您可以使用email_address->employee搜索电子邮件。它将确保通过电子邮件更快地查找员工以及持有额外地图的范围。如果我是你,我会选择这个选择。