Error validating login

Asked

Viewed 98 times

1

I have the following code:

package br.edu.utfpr.exer07;

    /*
    7) Implementar uma interface que contenha os métodos à ser usados:
    a. double soma(double valor1, double valor2, double valor3);
    b. String calculaTabuada(double tabuada, double valorInicial, double
    valorFinal);
    c. boolean login(String login, String senha);
    Implementar uma classe utilizando encapsulamento para armazenar cada tipo
    dos valores de retornos desta interface e que esta classe implemente esta
    interface e que contenha mais os métodos para apresentar cada um dos
    valores.
     */

public interface Calculos {

    public double soma(double valor1, double valor2, double valor3);
    public String calculaTabuada(double tabuada, double valorInicial, double valorFinal);
    public boolean login(String login, String senha);       
}

package br.edu.utfpr.exer07;

/*
7) Implementar uma interface que contenha os métodos à ser usados:
a. double soma(double valor1, double valor2, double valor3);
b. String calculaTabuada(double tabuada, double valorInicial, double
valorFinal);
c. boolean login(String login, String senha);
Implementar uma classe utilizando encapsulamento para armazenar cada tipo
dos valores de retornos desta interface e que esta classe implemente esta
interface e que contenha mais os métodos para apresentar cada um dos
valores.
 */

public class ExecutaCalculos implements Calculos {

    private double valor1;
    private double valor2;
    private double valor3;
    private double tabuada;
    private double valorInicial;
    private double valorFinal;
    private String login = "adm";
    private String senha = "123";

    public double getValor1() {
        return valor1;
    }

    public void setValor1(double valor1) {
        this.valor1 = valor1;
    }

    public double getValor2() {
        return valor2;
    }

    public void setValor2(double valor2) {
        this.valor2 = valor2;
    }

    public double getValor3() {
        return valor3;
    }

    public void setValor3(double valor3) {
        this.valor3 = valor3;
    }

    public double getTabuada() {
        return tabuada;
    }

    public void setTabuada(double tabuada) {
        this.tabuada = tabuada;
    }

    public double getValorInicial() {
        return valorInicial;
    }

    public void setValorInicial(double valorInicial) {
        this.valorInicial = valorInicial;
    }

    public double getValorFinal() {
        return valorFinal;
    }

    public void setValorFinal(double valorFinal) {
        this.valorFinal = valorFinal;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    @Override
    public double soma(double valor1, double valor2, double valor3) {
        return valor1 + valor2 + valor3;
    }

    @Override
    public String calculaTabuada(double tabuada, double valorInicial, double valorFinal) {
        for(double i=  valorInicial; i<=valorFinal; i++) {
            System.out.println(tabuada+" X "+i+" = "+tabuada*i);
        }
        return null;
    }

    @Override
    public boolean login(String login, String senha) {
        boolean estado;
        if(login == this.login && senha == this.senha) {
            System.out.println("Login efeituado com sucesso");
            estado = true;
        } else {
            System.out.println("Login e/ou senha inválidos");
            estado = false;
        }
        return estado;
    }
}

package br.edu.utfpr.exer07;

import javax.swing.JOptionPane;

public class Exer07 {

    public static void main(String[] args) {


        ExecutaCalculos ec = new ExecutaCalculos();

        //ec.setLogin(JOptionPane.showInputDialog("Login: "));
        //ec.setSenha(JOptionPane.showInputDialog("Senha: "));

        if(ec.login((JOptionPane.showInputDialog("Login: ")), (JOptionPane.showInputDialog("Senha: "))) == true) {
            System.out.println("| 1 - Somar            |"
                             + "| 2 - Calcular tabuada |");
            int opcao = Integer.parseInt(JOptionPane.showInputDialog(("1 - Soma\n"
                                                                    + "2 - Calcular tabuada\n"
                                                                    + "Opção: ")));
            switch(opcao) {
            case 1:
                ec.setValor1(Double.parseDouble(JOptionPane.showInputDialog("Informe o primeiro valor:")));
                ec.setValor2(Double.parseDouble(JOptionPane.showInputDialog("Informe o segundo valor:")));
                ec.setValor3(Double.parseDouble(JOptionPane.showInputDialog("Informe o terceiro valor:")));
                System.out.println(ec.soma(ec.getValor1(), ec.getValor2(), ec.getValor2()));
                break;
            case 2:
                ec.setTabuada(Double.parseDouble(JOptionPane.showInputDialog("Informe a tabuada que deseja calcular:")));
                ec.setValorInicial(Double.parseDouble(JOptionPane.showInputDialog("Informe o valor inicial:")));
                ec.setValorFinal(Double.parseDouble(JOptionPane.showInputDialog("Informe o valor final")));
                ec.calculaTabuada(ec.getTabuada(), ec.getValorInicial(), ec.getValorFinal());
            }
        }

    }

}

And in executing that if in class main, calling the method login class ExecutarCalculos, he always returns false. What could be the problem?

if(ec.login((JOptionPane.showInputDialog("Login: ")), (JOptionPane.showInputDialog("Senha: "))) == true)
  • 1

    For objects (i.e., non-priminal types), the == checks if the two point to the same memory space. So, how are you comparing strings , utilize equals.

  • Great, how come I didn’t think of it before? hahahaha Thank you so much

  • Okay, includes an answer just to let it record even =D

  • I didn’t notice it was the same person hehehehe

1 answer

2


It’s a common mistake that you have, I believe that everyone has been through it =)

In java when == is used to compare two objects it checks if both objects point to the same memory space. An example with String would be this:

final String string1 = new String("String");
final String string2 = "String";
final String string3 = string1;
final String string4 = "String";
if (string1 == string2) {
    System.out.println("string1 e string2 são o mesmo objeto.");
} else {
    System.out.println("string1 e string2 NÃO são o mesmo objeto.");
}
if (string1.equals(string2)) {
    System.out.println("string1 e string2 possuem o mesmo conteúdo, mas não o mesmo.");
} else {
    System.out.println("string1 e string2 NÃO possuem o mesmo conteúdo, mas não o mesmo.");
}
if (string1 == string3) {
    System.out.println("string1 e string3 são o mesmo objeto.");
} else {
    System.out.println("string1 e string3 NÃO são o mesmo objeto.");
}
if (string1 == string4) {
    System.out.println("string1 e string4 são o mesmo objeto.");
} else {
    System.out.println("string1 e string4 NÃO são o mesmo objeto.");
}
if (string2 == string4) {
    System.out.println("string2 e string4 são o mesmo objeto.");
} else {
    // dead code, sempre será igual
    System.out.println("string2 e string4 NÃO são o mesmo objeto.");
}
if (string3.equals(string4)) {
    System.out.println("string3 e string4 possuem o mesmo conteúdo, mas não o mesmo.");
} else {
    System.out.println("string3 e string4 NÃO possuem o mesmo conteúdo, mas não o mesmo.");
}

That would result in this:

string1 e string2 NÃO são o mesmo objeto.
string1 e string2 possuem o mesmo conteúdo, mas não o mesmo.
string1 e string3 são o mesmo objeto.
string1 e string4 NÃO são o mesmo objeto.
string2 e string4 são o mesmo objeto.
string3 e string4 possuem o mesmo conteúdo, mas não o mesmo.

That is, we know that always the content of String is the same, ie, String. However we have in this case two different objects, in other words, we occupy two different memory spaces with the same content. Any combination with the equals will always be the same, but with the == No, since you’re in string1 and string2 or string4 will be false.

In these cases the method is usually used equals of Object which checks the fairness of content when overwritten. By default only checks whether they point to the same memory space.

  • Thanks for the explanation!

Browser other questions tagged

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