Search code examples
loopswhile-loopfieldfillinfinite

[JAVA Infinite loop while requiring to fill empty fields


I've been trying to handle possible errors in my program with while loop to show message dialog says" fill the empties" but it does that for infinite. Simply, When I try to add something (such as contact) and there are some fields all must be filled how can I make sure of that?

Here's my code, works good to find the unfilled fields but it runs infinitely!

    public void createContact(){
    final JFrame addContact = new JFrame("Add Contact");
    addContact.setSize(400,300);
    addContact.setLayout(new BorderLayout());
    addContact.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JPanel info = new JPanel(new GridLayout(6,2));
    JButton add = new JButton("Add");
    JLabel title = new JLabel("Title: ");
    JLabel firstname = new JLabel("FIRST NAME: ");
    JLabel lastname = new JLabel("LAST NAME: ");
    JLabel adress = new JLabel("Adress: ");
    JLabel email = new JLabel("Email: ");
    JLabel company = new JLabel("Company Name: ");
    final JTextField title1 = new JTextField(20);
    final JTextField firstname1 = new JTextField(20);
    final JTextField lastname1 = new JTextField(20);
    final JTextField adress1 = new JTextField(20);
    final JTextField email1 = new JTextField(20);
    final JTextField company1 = new JTextField(20);
    info.add(title);
    info.add(title1);
    info.add(firstname);
    info.add(firstname1);
    info.add(lastname);
    info.add(lastname1);
    info.add(adress);
    info.add(adress1);
    info.add(email);
    info.add(email1);
    info.add(company);
    info.add(company1);
    addContact.add(info,BorderLayout.NORTH);
    addContact.add(add,BorderLayout.SOUTH);

    add.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            while(title1.getText().length() == 0 || firstname1.getText().length() == 0 || firstname1.getText().length() == 0 || adress1.getText().length() == 0 || email1.getText().length() == 0 ||company1.getText().length() ==0){
            JOptionPane.showMessageDialog(null,"Please fill the empty fields.");
            }
            Contact newContact = new Contact(title1.getText(),firstname1.getText(),lastname1.getText(),adress1.getText(),email1.getText(),company1.getText());
            contactList.add(newContact);
            JOptionPane.showMessageDialog(null,"Contact has been added successfuly!");
            addContact.setVisible(false);

        }
    });
    addContact.pack();
    addContact.setVisible(true);

}

Solution

  • Since it is in a while block, it will always run continuously (as the conditions are always met). But you cannot modify anything, since as soon you confirm the message, i will pop up again. Try with 'if' instead:

    add.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            if (title1.getText().length() == 0 || firstname1.getText().length() == 0 || firstname1.getText().length() == 0 || adress1.getText().length() == 0 ||    email1.getText().length() == 0 ||company1.getText().length() ==0){
                JOptionPane.showMessageDialog(null,"Please fill the empty fields.");
            } else {
                Contact newContact = new Contact(title1.getText(),firstname1.getText(),lastname1.getText(),adress1.getText(),email1.getText(),company1.getText());
                contactList.add(newContact);
                JOptionPane.showMessageDialog(null,"Contact has been added successfuly!");
                addContact.setVisible(false);
            }
        }
    }