Display only results that satisfy the condition within the loop

Asked

Viewed 41 times

0

I’m building a little algorithm that runs through a ArrayList and compares the value found with a value typed by the user.

But I want to print on the screen only the value that satisfies the operation, and not the other values that do not correspond to 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 Execucao {

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

Watch, he’ll be writing "Input does not satisfy" until you find the correct position on array. I am fully aware that this is the fault of the loop, which will run until the last position, but as the iteration variable is local, I do not know how to do outside the loop.

1 answer

3


It was not very clear to me on what occasions the "input satisfies", but starting only from what you did, I believe that we can eliminate the unwanted data only denying 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 make if get 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 can avoid displaying multiple non-valid entries, but if you want to display if nothing meets the if condition, you can use a counter, which you can serve in the future if you want to know how many entries are valid. If only one is always valid or no counter is required, 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");
}
  • It really worked with this trick of the variable Boolean, the next times I will pay attention to these details of use of the platform, thank you!

Browser other questions tagged

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