-1

Basically this is the action I want to achieve:

Please type your new password:
1234
Please enter your password:
1234
This password is correct.
(And Vice Versa)

Everytime I launch or edit this code, all I get is "This password is incorrect" P.S: I'm new to all this! This is my main class:

package PasswordValidation;
import java.util.Scanner;

public class Main {
public static void main(String[] args){
    Scanner kbrdIn = new Scanner(System.in);

    PasswordDB pwdObject = new PasswordDB();
        System.out.println("Please type your new password: ");

    String userPwd;
        userPwd = kbrdIn.nextLine();

    pwdObject.setPwd(userPwd);
    pwdObject.checkPwd();

        kbrdIn.close();
    }
}

This is my PasswordDB.java class:

package PasswordValidation;
import java.util.Scanner;

class PasswordDB {

Scanner pwdInput = new Scanner(System.in);

private String passWord;

public void setPwd(String name){
    passWord = name;
}   

public String getPwd(){
    return passWord;
}
public final void checkPwd(){

    System.out.println("Please enter the password: ");
        String pwdIn = pwdInput.nextLine();


if(pwdIn == passWord){

    System.out.println("Your password is correct.");
}else{

    System.out.println("Your password is incorrect.");

    }
  }
}

1 Answers1

0

In Java, you should NEVER USE == to check the equality of strings. String objects are considered not equal even if they contain the same value. That is because of the way Java handles objects in general.

Fortunately, Java has a built-in method for this. pwdIn.equals(password)

Blue
  • 545
  • 3
  • 13