Display only results that satisfy the condition within the loop
I am building a small algorithm that traverses a ArrayList
and compares the found value with a value typed by the user.
But I want it to be printed on the screen only the value that satisfies the operation, and not the other values that do not correspond to those that have been 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();
}
Notice, it will keep typing "input does not satisfy" until it finds 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.
1 answers
It was not very clear to me on which occasions the "input satisfies", but based only on what it did, I believe that to eliminate the unwanted data zaida only by 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 it does not satisfy, so by denying the result, I make the if Get true and display the desired text, or whatever you intend to display only when the input is valid.
Only with the change I suggested already avoids displaying invalid entries several times, but if you want to display if nothing meets the If condition, you can use a counter, which can be used in the future if you want to know how many entries are valid. If only one is always valid or does not need a counter, then you can use a variable of type boolean
, just to check out of 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");
}