How to check if the fields are empty before adding to the arraylist?

Asked

Viewed 411 times

0

I have a question: how could I check if the fields of my Jtextfields are empty before adding to the arraylist? as would be the validation in this part of code?

                Endereco enderecoPJ=new Endereco();
                PessoaJuridica pessoaJuridica=new PessoaJuridica();


                pessoaJuridica.setNome(tfNome.getText());
                pessoaJuridica.setSobrenome(tfSobrenome.getText());
                pessoaJuridica.setCnpj((Long.valueOf(tfIndentificador.getText())));


                enderecoPJ.setBairro(tfBairro.getText());
                enderecoPJ.setCep(Long.valueOf(tfCep.getText()));
                enderecoPJ.setCidade((String)cbCidades.getSelectedItem());
                enderecoPJ.setNum(Integer.valueOf(tfNum.getText()));
                enderecoPJ.setRua(tfRua.getText());

                pessoaControler.pj.add(pessoaJuridica);

                JOptionPane.showMessageDialog(null, "Cadastrado com Sucesso !");
  • I believe that the answer to this question marked as duplicate is the solution. In short, just apply trim() and then validate in an if with isEmpty()

  • I believe the question marked as duplicate is not very applicable in my code, I want to check all fields, after checking, if any field has empty I can not add in arraylist, otherwise it add

  • The logic applied in the two answers is exactly this. In one of them I even made a point about how to validate this.

1 answer

-1

You can create a method to check this.

In it you will pass as parameter your Jtextfields and check whether it is empty or not.

Then you need to make an if for each Jtextfields you want to add in the array.

It is a very simple logic, if you have doubts about how to do can comment here that we try to help.

Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.