2017-03-07 70 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