2012-07-17 86 views
0

嘿,我有一個EmployeeStore,我已經使用了一個hashmap。地圖存儲的變量是電子郵件名稱和ID。我有一個名爲SearchByEmail的方法,但是這有一個問題。當用戶向用戶界面輸入正確的員工電子郵件時,該方法返回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; 

    } 

回答

1

首先,

​​

確實沒有意義。 mapHashmap,並且employeeEmailString。他們在什麼條件下是平等的?

由於您既沒有包含地圖的聲明,也沒有插入新值的代碼,因此您不清楚地圖中存儲的內容以及如何存儲。我現在假設您存儲映射如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; 
} 
+0

我試過這個,我即將得到一個空白的答案。 – Pendo826 2012-07-17 11:20:38

+0

澄清我的答案。 – aioobe 2012-07-17 11:35:38

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)搜索電子郵件。它將確保通過電子郵件更快地查找員工,並確保持有額外的地圖。如果我是你,我會選擇這個選擇。
+0

如果您所知道的只是電子郵件地址,那麼'Map.containsValue'可能會有問題。 – aioobe 2012-07-17 11:08:15

+0

假設地圖類型正確。我也嘗試了你的建議,但我沒有喜悅:(輸出仍然返回false。 – Pendo826 2012-07-17 11:08:41

+0

@ Pendo826:「map」的*類型*究竟是什麼?它是什麼映射? – amit 2012-07-17 11:09:10