错误的字典输出

时间:2017-03-07 05:54:34

标签: java

我为此代码输出错误。当我搜索密钥时,它总是给我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

它应该给我详细信息Cliff ..但相反,它给了我Kristen的信息。并且,它适用于我搜索的任何数字。

谢谢!

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


}

1 个答案:

答案 0 :(得分: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