Alert only appear 1x inside a for

Asked

Viewed 47 times

-1

I have a question:

I have a method that goes through a list of clients and in it checks if it is the type countSolidaria =3 , every time the method goes through this list he gives an Alert showing one to the user with a joptionpane that he and Solidario.

I would like to know how to do for that same method go through the list but only show once to the user that message only once, once he went through the list.

private static final short SOLIDARIA = (short) 3;

clienteController = new ClienteController(session);

        List<ContaCorrenteModel> contaCorrenteModelList = new ArrayList<ContaCorrenteModel>();

        contaCorrenteModelList = clienteController.teste(Short.valueOf(Short.parseShort(banco)),
        Short.valueOf(Short.parseShort(agencia)), Long.valueOf(Long.parseLong(conta)));

        for (ContaCorrenteModel contaCorrenteModel : contaCorrenteModelList) {
            if (contaCorrenteModel.getNrSequNatzCnta().getCdNatzCnta() == SOLIDARIA) {
                JOptionPane.showMessageDialog(null, "A CONTA E TIPO 3");
            }

        }

    }

Just limit to the JOPTIONPANE. appear once , someone would have some idea how to do this?

  • 1

    Please supply one [mcve] so that we can test the code and propose a solution.

  • This code is not executable, has several libs there q are not part of java

2 answers

0

Try to do it this way:

clienteController = new ClienteController(session);
int contador = 0;

List<ContaCorrenteModel> contaCorrenteModelList = new ArrayList<ContaCorrenteModel>();

contaCorrenteModelList = clienteController.teste(Short.valueOf(Short.parseShort(banco)),
Short.valueOf(Short.parseShort(agencia)), Long.valueOf(Long.parseLong(conta)));

for (ContaCorrenteModel contaCorrenteModel : contaCorrenteModelList) {
    if (contaCorrenteModel.getNrSequNatzCnta().getCdNatzCnta() == SOLIDARIA) {
        contador++;
    }

}

if (contador > 0) {
    JOptionPane.showMessageDialog(null, "A CONTA E TIPO 3");
}

Solution: Create a counter to check if the account enters the desired condition and after the for check that the counter is greater than 0. If it is greater than 0 it is because it has found a user of the desired type.

0

Use a variable boolean to determine whether the message has already been shown or not

boolean mennsagemMostrada = false;
for (ContaCorrenteModel contaCorrenteModel : contaCorrenteModelList) {
    if (contaCorrenteModel.getNrSequNatzCnta().getCdNatzCnta() == SOLIDARIA) {
        if (!mensagemMostrada) {
            JOptionPane.showMessageDialog(null, "A CONTA E TIPO 3");
            mensagemMostrada = true;
        }
    }
}

Browser other questions tagged

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