嵌套如果语句失败

时间:2011-10-16 15:24:24

标签: java generics if-statement

我来自C背景,所以我假设我的语法不正确。

在以下代码中;

    public class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        String jcbValue = (String) jcbIDF.getSelectedItem();
        if (jcbValue.equals("Insert")) {

            String Id = jtfId.getText();
            ArrayList<String> ValueList = new ArrayList<String>();
            String Name = jtfName.getText();
            String GPA = jtfGPA.getText();
            ValueList.add(Name);
            ValueList.add(GPA);
            if (map.containsKey(Id)){
                JOptionPane.showMessageDialog(null, "Key exists!",
                        "Result", JOptionPane.INFORMATION_MESSAGE);
            }   
            else if (!map.containsKey(Id)){

                map.put(Id, ValueList);
                System.out.println(map);

                JOptionPane.showMessageDialog(null, "Record inserted",
                        "Result", JOptionPane.INFORMATION_MESSAGE);
                jtfId.setText("");
                jtfName.setText("");
                jtfGPA.setText("");
            }

        } //terminates insert
        else if (jcbValue.equals("Delete")) {
            String Id = jtfId.getText();
            ArrayList<String> ValueList = new ArrayList<String>();
            String Name = jtfName.getText();
            String GPA = jtfGPA.getText();

            if (map.containsKey(Id)){
            JOptionPane.showMessageDialog(null, "Key exists, deleted!",
                        "Result", JOptionPane.INFORMATION_MESSAGE);
            } else if (!map.contasKey(Id)){      
            System.out.println(map);
            JOptionPane.showMessageDialog(null, "Key Does not exist!",
            }                                                       


        } //terminates delete
        else if (jcbValue.equals("Find")) {
            System.out.println(map);
            JOptionPane.showMessageDialog(null,
                    "Find Selected; But not Implemented", "Result",
                    JOptionPane.INFORMATION_MESSAGE);
        } //terminates find

    }// Terminates actionPerformed Class
}// Terminates ButtonListenerClass

我收到编译错误,说“} X行意外,早期EOF。如果我删除了评估map.containsKey(Id)的子IF,它编译并运行正常。我在互联网上读到的一切都说Java有能力嵌套IF语句,那我到底做错了什么?

感谢您的帮助!

CJ

2 个答案:

答案 0 :(得分:6)

这一行?

        JOptionPane.showMessageDialog(null, "Key Does not exist!",
        }

您的方法调用未关闭。我想你打算这样做:

        JOptionPane.showMessageDialog(null, "Key Does not exist!",
                                      "Result", JOptionPane.INFORMATION_MESSAGE);
        }

答案 1 :(得分:1)

您的问题出在上一个else if声明中:

JOptionPane.showMessageDialog(null, "Key Does not exist!",

此行未关闭。就像之前的一张海报所说,你应该做点什么,

JOptionPane.showMessageDialog(null, "Key Does not exist!", "Result", JOptionPane.INFORMATION_MESSAGE);