hashmap没有被新输入更新

时间:2017-05-04 14:15:19

标签: hashmap

当用户决定更新hashmap时,我的代码无法正确更新。我的代码的更新部分是这个

    if(comboBoxSelection == "Update"){

        String gradeValue= (String) JOptionPane.showInputDialog(null, "Choose Grades: ", "", JOptionPane.INFORMATION_MESSAGE, null, grades, "");
        String creditAmount=  (String) JOptionPane.showInputDialog(null, "Choose Credit: ", "", JOptionPane.INFORMATION_MESSAGE, null, credits, "");

        studentObj.courseCompleted(gradeValue.charAt(0), Integer.parseInt(creditAmount));

        studentDatabase.put(iD, studentObj);


    }//end update combobox selection

并且接受这些更改的方法是

public void courseCompleted(char courseGrade, int creditHours){

    if (courseGrade == 'A'){
        totalCredit = 4;
        totalQuailtyPoints = (totalCredit * creditHours);
    }//end course grade A

    if(courseGrade == 'B'){
        totalCredit = 3;
        totalQuailtyPoints = (totalCredit * creditHours);

    }//end course grade B

    if(courseGrade == 'C'){
        totalCredit = 2;
        totalQuailtyPoints = (totalCredit * creditHours);

    }//end course grade C

    if(courseGrade == 'D'){
        totalCredit = 1;
        totalQuailtyPoints = (totalCredit * creditHours);

    }//end course grade D

    if(courseGrade == 'F'){
        totalCredit = 0;
        totalQuailtyPoints = (totalCredit * creditHours);

    }//end course grade F


}//end courseCompleted method

如果我需要发布更多代码,请告诉我

2 个答案:

答案 0 :(得分:0)

从技术上讲,你的hashMap应该更新。您从JOptionPane获得的gradeValue有可能与任何情况都不匹配。或者永远不会执行该代码块。插入一些println语句来检查上面的内容。

if(comboBoxSelection == "Update")

这不是比较字符串的方法。

使用equals()方法。因此,甚至可能无法调用更新块。

if(comboBoxSelection.equals("Update"))

答案 1 :(得分:0)

我想我想通了。所以由于某种原因添加此代码将显示我正在寻找的信息,它将允许哈希映射更新。新代码是

if(comboBoxSelection.equals("Update")){

        if(studentDatabase.containsKey(iD)){

        studentObj = studentDatabase.get(iD);
        String gradeValue= (String) JOptionPane.showInputDialog(null, "Choose Grades: ", "", JOptionPane.INFORMATION_MESSAGE, null, grades, "");
        String creditAmount=  (String) JOptionPane.showInputDialog(null, "Choose Credit: ", "", JOptionPane.INFORMATION_MESSAGE, null, credits, "");

        studentObj.courseCompleted(gradeValue.charAt(0), Integer.parseInt(creditAmount));

        studentDatabase.put(iD, studentObj);


        }//end makes sure key exists

        else{
            JOptionPane.showMessageDialog(null, "Student does not exist");
        }//end else

    }//end update combobox selectio