2017-03-07 68 views
-4

我一直在爲這段代碼弄錯輸出。當我搜索密鑰時,它總是給我最後一個txt文件的值。 例如: 這是我的文本文件...本字典輸出錯誤

Sam,123 Main Line,555-0469,123-45-6789,2423.07 
Carla,456 Off Line,555-0101,987-65-4321,1246.15 
Woody,789 Off Rocker,555-0000,010-20-3040,1169.23 
Diane,678 Fifth Ave,555-0690,958-47-3625,10.55 
Norm,987 Suds Blvd.,555-8374,456-78-9000,11.2 
Cliff,321 Duds Lane,555-7282,621-12-1234,12.0 
Tom,2631 Main Blv,423-1155,524-332-6654,10.0 
Kristen,443 Norfolk str,765-9457,010-332-1111,20.0 

當我嘗試例如搜索:# 621-12-1234

它應該給我崖的細節..但相反,它給了我克里斯汀信息。而且,它可以用於我搜索的任何號碼。

謝謝!

static Map<String,StaffMember>dictionary1=new HashMap<>(); 
    static Map<String,StaffMember>dictionary2=new HashMap<>(); 
    public static void main(String[] args) throws Exception{ 
     addThemAll(); 
     searchFor(); 
    } 
    public static void addThemAll() throws Exception 
    { 
     FileReader file = new FileReader("employee.txt"); 
     BufferedReader br = new BufferedReader(file); 
     StaffMember staff = new StaffMember(); 
     String line = null; 
     while ((line = br.readLine()) != null) 
     { 
      String[] lineSplit = line.split(","); 
      Double rate1=Double.parseDouble(lineSplit[4]); 
      staff.SetStaffMember(lineSplit[0], lineSplit[1], lineSplit[2], lineSplit[3], rate1); 
      dictionary1.put(lineSplit[3], staff); 
      dictionary2.put(lineSplit[0], staff); 
     }//end of while loop 
    }//end of addThemALL Class 

    public static void searchFor() 
    { 
     Scanner scan= new Scanner(System.in); 
     System.out.println("Enter SSN you want to search"); 
     String SSN=scan.nextLine(); 
     StaffMember staff1=dictionary1.get(SSN); 
     if(dictionary1.containsKey(SSN)) 
     System.out.println(dictionary1.get(SSN)); 
    } 


} 
+0

豎起烏爾文本文件了。 – Smit

+2

爲什麼在調用'containsKey'之前獲取密鑰? –

回答

0

您只將一個對象放入地圖中。

StaffMember staff = new StaffMember(); 
    String line = null; 
    while ((line = br.readLine()) != null) 
    { 

你需要許多對象

String line = null; 
    while ((line = br.readLine()) != null) 
    { 
     StaffMember staff = new StaffMember(); // Move into loop 
+0

謝謝cricket_007!那是做的... – mike

+0

我做了希望它工作! – mike