Alert appears only 1x within a for

-1

I have a question:

I have a method that walks through a list of clients and in it checks if it is of type accountSolidaria = 3, every time the method goes through this list it gives an alert showing a user with a joptionpane that he is supportive. p>

I would like to know how to do this same method scrolls through the list but only once to show the user this message once once it has gone 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");
            }

        }

    }

Only limit to JOPTIONPANE. to appear once, would anyone have any idea how to do this?

    
asked by anonymous 24.07.2018 / 16:11

2 answers

1

Try to do this:

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 for check if the counter is greater than 0. If it is greater than 0 it is because it found a type.

    
24.07.2018 / 16:27
0

Use a boolean variable 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;
        }
    }
}

If the test is only to show the message, only a if can be used as in

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

Only one example, the variable name can be modified, including the inverted meaning, type boolean primeiraVez = true; - depends on the project, personal taste, ...

    
24.07.2018 / 20:22