2017-05-04 111 views
0

我遇到了一個問題,當用戶決定更新散列映射時,我的代碼沒有被正確更新。我的代碼的更新部分是這個散列表未被新輸入更新

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 

,如果我需要發佈更多的代碼,請讓我知道

回答

0

技術上你的HashMap應該更新。有可能您從JOptionPane獲得的gradeValue與任何情況都不匹配。或者該代碼塊從未執行。插入一些println語句來檢查上面的事情。

if(comboBoxSelection == "Update") 

這不是比較字符串的方法。

使用equals()方法。正因爲如此,你的更新塊甚至可能不會被調用。

if(comboBoxSelection.equals("Update"))

0

我想我想通了。所以出於某種原因,添加此代碼將顯示我正在查找的信息,並允許hashmap更新。新代碼是

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