Display only results that satisfy the condition within the loop

0

I am building a small algorithm that traverses a ArrayList and compares the value found with a value entered by the user.

But I want to print on the screen only the value that satisfies the operation, not the other values that do not match those that were compared:

   public class Hash {

HashMap<String, String> mapaPosicoes = new HashMap<>();
ArrayList<String> listaPosicoes = new ArrayList<>();
Scanner teclado = new Scanner(System.in);

String posicaoJogador, nomeJogador;

public void pegaDadosJogador() {
    System.out.print("Digite a posição do jogador: ");
    posicaoJogador = teclado.nextLine();

    System.out.print("Digite o nome do jogador: ");
    nomeJogador = teclado.nextLine();
}

public void validadorDadosJogador() {
    listaPosicoes.add("Atacante");
    listaPosicoes.add("Cortador");
    listaPosicoes.add("Levantador");
    listaPosicoes.add("Bloqueador");
    listaPosicoes.add("Jogador de Defesa");
    listaPosicoes.add("Líbero");

    for(String iteracao: listaPosicoes) {
        if(!(!iteracao.equals(posicaoJogador) || nomeJogador.isEmpty())) {
            mapaPosicoes.put(nomeJogador, posicaoJogador);
            System.out.println("Jogador(es) Mapeado(s)");
            System.out.println(mapaPosicoes);
        }else {
            System.out.println("A");
        }
    }
}

public class Executing {

public static void main(String[] args) {
    Hash time = new Hash();
    time.pegaDadosJogador();
    time.validadorDadosJogador();
}

Please note, it will be typed "Input not satisfying" until you find the correct position in the array . I am fully aware that this is the fault of the loop, which will be running until the last position, but since the iteration variable is local, I do not know how to do it outside the loop.     

asked by anonymous 09.09.2018 / 22:24

1 answer

3

It was not very clear to me on what occasions "input satisfies", but starting from what you did, I think you can eliminate unwanted data loss by just negating the condition inside the if:

for(String iteracao: listaPosicoes) {
    if(!(!iteracao.equals(posicaoJogador) || nomeJogador.isEmpty())) {
        System.out.println("Entrada satisfaz");
    }
}

When the condition inside the if is true, it means that it does not satisfy, so by denying the result, I have if the if true and display the desired text, or whatever you want to display only when the entry is valid.

Only with the change I suggested, you already avoid displaying invalid entries several times, but if you want to display if nothing meets the condition of if, you can use a counter, which can serve in the future if you want to know how many entries are are valid. If only one is always valid or not, then you can use a variable of type boolean , just to check out the loop when something is found:

boolean achou =  false;

...

for(String iteracao: listaPosicoes) {
    if(!(!iteracao.equals(posicaoJogador) || nomeJogador.isEmpty())) {
        mapaPosicoes.put(nomeJogador, posicaoJogador);
        System.out.println("Jogador(es) Mapeado(s)");
        System.out.println(mapaPosicoes);
        achou = true;
    }
}

...

if(!achou) {
    System.out.println("Nenhuma das entradas satisfaz");
}
    
09.09.2018 / 22:31