0

ねえ、私はこれにハッシュマップを使用したEmployeeStoreを持っています。マップが格納する変数は、電子メールの名前と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;

    }
4

2 に答える 2

1

初めに、

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

次に、の従業員が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<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 に答える