void无法转换为boolean if语句错误

时间:2016-03-28 03:30:45

标签: java

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        try 
        {
            DVD dvd = new DVD(txtTitle.getText(), 
                    Integer.parseInt(txtYear.getText()),
                    true);

            if(collection.addDVD(dvd))
                JOptionPane.showMessageDialog(this, "dvd added successfully");

基本上我需要在关闭时保存我的dvd集合,但if语句给我的void错误无法转换为boolean。

这是addDvd方法

public void addDVD(DVD inDVD) 
    {
        dvds.add(inDVD);
        inDVD.setID(nextID);
        nextID++;
    }

无法更改void addDvd方法,因为它是我老师提供的......

2 个答案:

答案 0 :(得分:-1)

为什么要尝试测试addDVD()的返回结果?你可以这样做:

collection.addDVD(dvd);
JOptionPane.showMessageDialog(this, "dvd added successfully");

答案 1 :(得分:-1)

试试这个:

try {
// do all the work as above
// remove if statement 
collection.addDVD(dvd);
JOptionPane.showMessageDialog(this, "dvd added successfully");
}
catch {
// catch the error here 
JOptionPane.showMessageDialog(this, "dvd not added successfully");
}