2017-05-04 1 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

당신의 해시 맵을 알려 주시기 바랍니다 업데이트해야합니다. JOptionPane에서 가져온 gradeValue가 어떤 경우와도 일치하지 않을 가능성이 있습니다. 또는 해당 코드 블록이 실행되지 않습니다. println 문을 삽입하여 위의 내용을 확인하십시오.

if(comboBoxSelection == "Update") 

이것은 문자열을 비교하는 방법이 아닙니다.

equals() 메소드를 사용하십시오. 이 때문에 업데이트 블록이 호출되지 않을 수도 있습니다.

if(comboBoxSelection.equals("Update"))

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 
입니다.
관련 문제