Vector (array) log-in validation. java

Asked

Viewed 166 times

0

I’m studying vectors in object orientation and decided to do a little program to put some things into practice.

It consists of making the record of n vectors that will be bank accounts with name, password, Account and balance. Using only the console as an interaction, they should be registered at positions on the vector separately each new conta(name,password,account,balance).

Soon after, one should ask for a kind of "log-in" which consists of name and password and both must pass a basic and rustic validation process, which would first see if the name typed in the part of log-in corresponds to an account registered in the vector and if the answer is true give the option to type the password, and the validation of this will be to see if the vector with the name chosen has the password registered also to which 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();

    }

}

Here’s the problem, the compiler is not "respecting" the condition if(check = false){sysout("Nome inválido");}(...).

Regardless of what I do or type it gives as true and already executes the sysout{("Password: );}.

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

2 answers

5

Perhaps I am wrong, but I believe that comparisons must be made with == and not just =:

if(check == false)

== => Comparison

= => Assign value

5


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

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

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

It’s like you’re doing it in two steps:

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

Yes, note that when the variable is a boolean, I don’t need to compare it to true or false. The if tests a boolean value, and as the variable itself check is a boolean, I can check it directly. And how the value is true, he at all times will get into this if.

Of course I could check if her value equals true, making if (check == true), but for boolean variables this is redundant and unnecessary. Anyway, see that to compare values we use the operator ==, and not =.

That is to say, if (check = true) always gets you into this if - and therefore never reaches the if (check = false). Incidentally, that if is also wrong. If you want to test if a boolean is fake, just do if (! check) ("if check in is false").

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

if (check) {
    // check é verdadeiro (true)
} else {
    // check é falso (false)
}

Browser other questions tagged

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