java 字符串比较问题

来源:互联网 发布:淘宝客服绩效管理流程 编辑:程序博客网 时间:2024/05/22 00:07

今天写程序的时候,被一个问题纠结了很久。

源码如下:

public void changePassword() throws Exception{
System.out.println("["+this.USER_NAME+"]"+
"Please enter the password: ");
Scanner sc=new Scanner(System.in);
String password=sc.nextLine();
if(this.isPassword(password)){
System.out.println("["+this.USER_NAME+"]"+
"Please enter the new password: ");
}
else{
System.out.println("The password is wrong");
sc.close();
return;
}

while(true){
String newpassword=sc.nextLine();
if(newpassword.length()<10){
this.PASS_WORD=newpassword;
System.out.println("You have changed your password!");
sc.close();
break;
}
else
System.out.println("Illeagel new password!\n"
+ "It should be competely formed by digits or letters\n"
+"and the length of password should be less than 10\n"
+"\n"+"Please enter the new password: ");
}

}
public boolean isPassword(String password){
if(this.PASS_WORD==password)
return true;
else
return false;
}


发现总是 password is wrong 。

查看资料后发现,是在判断 isPassword这里出现了错误。

 == 符号 在比较字符串时 是 地址相同 才会 true。

而 使用 equals 函数则比较内容。

犯这么低级的错误真的是太不应该了!!

0 0