Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the variable J is always 0? For inside a For – Java

IntelliJ says "j++" is never used and I just can’t figure out why.

What I’m trying to do is check if exists 2 accounts of the type Conta searching by the their attribute "numero" with getNumero. And I just search for the second account if the first one exists.

PS: listaClientes is an ArrayList of the type Conta

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for(int i = 0; i < listaClientes.size(); i++){
                if(listaClientes.get(i).getNumero() == numContaOrigem){
                    System.out.println("Qual o numero da conta de destino? (TRANSFERÊNCIA)");
                    int numContaDestino = teclado.nextInt();
                    // -------- Here is the problem --------- vvvv --------------
                    for (int j = 0; j < listaClientes.size(); j++){
                        if(listaClientes.get(j).getNumero() == numContaDestino){
                            System.out.println("Qual o valor da transferencia?");
                            double valorTransf = teclado.nextDouble();
                            if(listaClientes.get(i).transferencia(valorTransf, listaClientes.get(j))){
                                System.out.println("Transferência realizada!");
                                return;
                            }else{
                                System.out.println("Saldo insuficiente! Transferência não realizada!");
                                return;
                            }
                        }else{
                            System.out.println("Cliente destino não encontrado!");
                            return;
                        }
                    }
                }else{
                    System.out.println("Cliente origem não encontrado!");
                    return;
                }
            }

>Solution :

for (int j = 0; j < listaClientes.size(); j++){
  if(listaClientes.get(j).getNumero() == numContaDestino){
    ...
  }else{
    System.out.println("Cliente destino não encontrado!");
    return; /* ! this return is why j++ is not used ! */
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading