Validation of log-in by vector (array) .java

0

I'm studying object-oriented vectors and I've decided to do some programming to get things done.

It consists of registering n vectors that will be bank accounts with name, password, account and balance . Using only the console as an interaction, they should be registered in positions in the vector separately each% w / w%.

Next, you should ask for a " log-in" which consists of name and password and both must pass a basic and rustic validation process, which would first be to see if the name typed in the log-in part corresponds to an account registered in the vector and if the answer is true option to enter the password , and the validation of this will be to see if the vector with the name chosen has the password also registered to that the user typed in log-in .

package application;

import java.util.Locale;
import java.util.Scanner;

import entities.ContaBancaria;

public class ProgramContaBancaria {

    public static void main(String[] args) {

        Locale.setDefault(Locale.US);
        Scanner sc = new Scanner(System.in);

        System.out.print("Digite quantas contas serão cadastradas: ");
        int quantidade = sc.nextInt();
        ContaBancaria[] conta = new ContaBancaria[quantidade];

        System.out.println();

        for(int i = 0; i < conta.length; i++) {
        int x = 1;
        x += i; 
        System.out.println("Conta #"+ x);
        System.out.print("Digite o nome da conta para cadastro: ");
        String name = sc.next();
        System.out.print("Digite a senha para cadastro: ");
        int password = sc.nextInt();
        System.out.print("Digite o número da conta para cadastro: ");
        int account = sc.nextInt();
        conta[i] = new ContaBancaria(name, password, account);
        System.out.println();
        }

        System.out.println("Clientes cadastrados com sucesso!");
        System.out.println();
        System.out.println("  Login");
        System.out.print("Name: ");
        String name = sc.next();
        boolean check = false;

        for(int i = 0; i < conta.length; i++) {
            if(conta[i].getName().equals(name) ) {
                    check = true;
            }
            else 
                    check = false;
            }

            if (check = true) {
            System.out.print("Password: ");
            }
            else if (check = false){
                System.out.print("Nome inválido: ");
                String name2 = sc.next();
                name += name2;
        }
        int password = sc.nextInt();

    }

}

So the problem is, the compiler is not "respecting" the new conta(name,password,account,balance) condition.

Regardless of what I do or type it gives as if(check = false){sysout("Nome inválido");}(...) and already executes true .

What's wrong? What could I do to improve / change?

    
asked by anonymous 26.12.2018 / 13:56

2 answers

5

The error has nothing to do with "the compiler is not respecting the condition". In fact the program is doing exactly what you asked for.

In this case, the if(check = true) snippet is doing the following:

  • assigning the value true to the variable check
  • testing this value

It's like you do it in 2 steps:

check = true;
if (check) {
    ....

Yes, note that when the variable is a boolean , I do not have to compare it with true or false . if tests a Boolean value, and since the check variable itself is boolean , I can check it directly. And since the value is true , it will always go into this if .

Of course I could check if its value is equal to true , making if (check == true) , but for Boolean variables this is redundant and unnecessary. However, for comparing values we use the == operator, not = .

That is, if (check = true) causes it to always enter this if - and therefore never reaches if (check = false) . In fact, this if is also wrong. If you want to test if a boolean is false, just do if (! check) ("if check is false").

But since Boolean can only have 2 values (true or false), you do not need to test the same variable again on else . Just do it like this:

if (check) {
    // check é verdadeiro (true)
} else {
    // check é falso (false)
}
    
26.12.2018 / 14:27
4

Maybe I'm wrong, but I think comparisons should be made with == and not just = :

if(check == false)

== = > Comparison

= = > Assign value

    
26.12.2018 / 14:18