0

I'm doing my first project in android studio, using java, and I'm having a problem. The purpose of the program is to verify that the old password, in this case "xxxx", corresponds to the password typed by the app user. But when running the program, this comparison always gives error, even if it is the same. Can you help me, please?

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class ProfileActivity extends AppCompatActivity {

    private EditText oldPass, newPass;
    private Button buttonChange;
    //private String oldPassstr, newPassstr; //tem a pass que o user vai inserir

    //@SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        newPass = (EditText) findViewById(R.id.editNewPassword);
        oldPass = (EditText) findViewById(R.id.editOldPassword);
        buttonChange = findViewById(R.id.change_but);

        goToHome();
        goToSmart();
        ChangePassword(oldPass);
    }

    public void goToHome(){
        ImageButton button = (ImageButton) findViewById(R.id.Profile_Home_but);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(ProfileActivity.this,myHomeActivity.class);
                startActivity(i);
            }
        });
    }

    public void goToSmart(){
        ImageButton button = (ImageButton) findViewById(R.id.Profile_Smart_but);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(ProfileActivity.this,SmartActivity.class);
                startActivity(i);
            }
        });
    }

    public void ChangePassword(EditText oldPass) {

        buttonChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (oldPass.getText().toString() == "xxxx") {
                    Context context1 = getApplicationContext();
                    Toast toast1 = Toast.makeText(getApplicationContext(), "Password Updated!", Toast.LENGTH_SHORT);
                    toast1.show();
                }
                else{
                    Context context2 = getApplicationContext();
                    Toast toast2 = Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT);
                    toast2.show();
                }
            }
        });
    }
}
stack
  • 1
  • 1
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – f1sh Nov 01 '22 at 23:03

1 Answers1

0

The answer

Hi. You should definitely not be using the == operator for String comparisons in Java.

Use the equals method instead: oldPass.getText().toString().equals("xxxx"). (But look out for NPEs! If you ever plan on using a variable instead of "xxxx", you should make sure it's not null, as the validation else wise would result in a NPE.

A little tip

Instead of always having to override an interface, such as View.OnClickListener, you can just use the lambda version of it.
(Lambdas allow you to code a functional method, as a replacement for an (anonymous) class as an implementation).

This would look as follows:

buttonChange.setOnClickListener(event -> { /* your code... */});

In combination with the equals solution, your code could look something like this:


buttonChange.setOnClickListener(event -> {

    if (oldPass.getText().equals("xxxx")) {
        Toast.makeText(getApplicationContext(), "Password updated!", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_SHORT).show();
    }
});

Some of the things I did:

  • I removed the unused Context context1 variable, as you're not using it anyways.
  • The toast1.show(); can be "executed" directly.
  • I removed the toString() from the equals statement, as a charsequence can be validated too.

(If you have more questions like this popping up in your head, I advise you to do some sort of beginner tutorial for Java in general).

Z-100
  • 518
  • 3
  • 19