Every time I run always print Invalid login

Asked

Viewed 67 times

2

/* Make an algorithm that checks the login and password entered by user is correct. The algorithm must request from the user: login and password, and check on a set of 3 stored information variables. If the user provides the correct data, the algorithm should print a message from "Login and password correct", otherwise "Invalid login or password". The algorithm will be completed if the user hits the information or misses 3 times. */

import java.util.Scanner;
public class exercicio5 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String login = "admin" ; String senha="1234"; int count = 1; String digite;

    while (count<=3) {
        System.out.print ("Digite o login:");
        digite = sc.next();
        if (login==digite) {
            System.out.print ("Digite a senha:\n");
            digite = sc.next();
            if (digite==senha){
                count=3;
            }
        } else  {
            System.out.print("Login invalido.\n\n");
        }
        count = count+1;
    }

    System.out.print("Concluido");


    }
}

3 answers

3

Try it this way :

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String login = "admin" ; String senha="1234"; int count = 1; String digite;

        while (count<=3) {
            System.out.print ("Digite o login:");
            digite = sc.next();
            if (login.equals(digite)) {
                System.out.print ("Digite a senha:\n");
                digite = sc.next();
                if (digite.equals(digite)){
                    count=3;
                }
            } else  {
                System.out.print("Login invalido.\n\n");
            }
            count = count+1;
        }

        System.out.print("Concluido");


    }

2

Use the equals method to compare string, as follows

if (login.equals(digite)) {

}

1

Use the String class method equals()

Notice how I rewrote your program:

import java.util.Scanner;
public class exercicio5 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String login = "admin" ; String senha="1234"; int count = 1; String digite;

    while (count<=3) {
        System.out.print ("Digite o login:");
        digite = sc.next();
        if (login.equals(digite)) {
            System.out.print ("Digite a senha:\n");
            digite = sc.next();
            if (digite.equals(senha)){
                count=3;
            }
        } else  {
            System.out.print("Login invalido.\n\n");
        }
        count = count+1;
    }

    System.out.print("Concluido");


    }
}

When you compare two objects using ==, you are comparing whether the two objects are equal, ie if they point to the same memory address. As objects are instances different of the same class, the best way to compare them is by using methods created for this purpose, such as the equals().

Browser other questions tagged

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