Android - String and varchar comparison

Asked

Viewed 300 times

1

I’m having trouble comparing strings. A retreat of a Edittext and the other of an SQL table. The app is a game like "Who wants to be a millionare?"

When it comes to the if cycle of comparison between the two strings:

if (pass_jogador == verifica_pass) {...

goes directly to the Else correspondent.

When I use the application and enter the data, I use a Toast to check if the two strings are equal:

 apresenta_toast(verifica_pass);
                apresenta_toast(pass_jogador);

and the result is the same, indicate to me that the strings are equal.

Here’s my code in full:

 int id = v.getId();

    if (id == btn_login.getId()) {
        //verificar se jogador existe
        //verificar a password
        //faz login
        String verifica_email = this.email.getText().toString();
        String verifica_pass = this.password.getText().toString();
        if (verifica_email != null && verifica_pass != null) {
            MyDBHelper dbHelper = new MyDBHelper(this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();

            Cursor cursor = db.rawQuery("SELECT * FROM Jogador WHERE email_jogador = '" + verifica_email + "'", null);

            if (cursor != null && cursor.moveToFirst()) {
                String pass_jogador = cursor.getString(2);
                //verifica a igualdade das passwords
                apresenta_toast(verifica_pass);
                apresenta_toast(pass_jogador);
                if (pass_jogador == verifica_pass) {
                    Jogador player = criaJogador(cursor, db);

                    Intent myIntent = new Intent(this, MenuActivity.class);
                    startActivity(myIntent);

Here the table and the data of the user I entered by default:

db.execSQL("CREATE TABLE Jogador (email_jogador VARCHAR(50) PRIMARY KEY, nome VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, genero VARCHAR(50) NOT NULL, status VARCHAR(50) NOT NULL, pontuacao INTEGER, perguntasC INTEGER)");



 db.execSQL("INSERT INTO Jogador(email_jogador, nome, password, genero, status, pontuacao, perguntasC) VALUES ('[email protected]', 'Zé', 'qwerty', 'masculino','dealer', 0, 0)");
  • 1

    You can use equals to compare the String if(pass_player.equals(verifica_pass))

  • @Rodrigo.oliveira thank you, this is really the answer you were looking for! :)

1 answer

5


Type String are objects and to compare you should use the equals to make the comparison between them.

if (pass_jogador.equals(verifica_pass) ){..}

I suggest you read this answer:

Browser other questions tagged

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