user13173235 :
I am new at programming. I want to make a program that checks the input name and password are same or not, if it is, the program must say "Your name and your password cannot be same".My code is like below but even both of two input are same or different, result is same. What am i doing wrong?
import java.util.Scanner;
public class project {
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter Your Name ");
String name =scan.nextLine();
System.out.println("Please Enter Yout Password for register");
String password=scan.nextLine();
if(name.equals(password.startsWith(password))) {
System.out.println("Your name and your password cannot be same");}
else {
System.out.println("Register is successful");
}
}
}
ᴇʟᴇvᴀтᴇ :
You should change this line:
if(name.equals(password.startsWith(password))) {
To:
if (name.equals(password)) {
Why it fails
It seems that you inadvertently added .startsWith(password)
into the condition.
Explanation about what's happening
The expression password.startsWith(password)
returns the boolean
value true
. Which causes the condition for the if statement to be: if(name.equals(true))
which always returns false
because a String
never equals()
a boolean
.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=385766&siteId=1