[英]String.length() gives me a wrong value
Whenever I enter a password under 10 characters it gives me Password cannot exceed 10 characters
. 每当我输入的密码少于10个字符时,
Password cannot exceed 10 characters
。
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String name = Name.getText();
String Username = uName.getText().toString();
String Pass1 = uPass.getPassword().toString();
String Confirm = uConfirm.getPassword().toString();
String Status = "OFFLINE";
int PassLen = Pass1.length();
if (Username.equals("") || Pass1.equals("") || Confirm.equals("") || name.equals(""))
{
JOptionPane.showMessageDialog(null, "You cannot leave any fields blank when creating an Account. Please Try Again");
}
else if ((uPass.getPassword().toString()).length()>10)
{
uPass.setText("");
uConfirm.setText("");
JOptionPane.showMessageDialog(null, "Password cannot exceed a maximum of 10 characters.");
}
else if (!Pass1.equals(Confirm))
{
uConfirm.setText("");
lblError1.setText("Passwords Do Not Match.");
lblError2.setText("Please re-enter your Password.");
}
else
{
try {
DB_Connect connect = new DB_Connect();
ResultSet rs = connect.queryTbl("SELECT * FROM ACOUNTS");
boolean AlreadyUser = false;
String User;
while (rs.next())
{
User = rs.getString("Username");
if(Username.equals(User))
{
AlreadyUser = true;
}
}
if (AlreadyUser==false)
{
connect.updateTbl("INSERT INTO NBUSER.ACCOUNTS (USERNAME,PASSWORD,STATUS,NAME)VALUES ('"+Username+"','"+Pass1+"','"+Status+"','"+name+"')");
JOptionPane.showMessageDialog(null, "Account Created Successfully !");
this.dispose();
new Topics().setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "The Username you have selected already exists. Please select a different Username");
uPass.setText("");
uConfirm.setText("");
}
} catch (SQLException ex) {
Logger.getLogger(CreateAccount.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Since you're obviously using Swing, it is also very likely that you use a JPasswordField
for your passwords. 由于您显然正在使用Swing,因此很可能使用
JPasswordField
作为密码。 So let's see, what getPassword
really does: 让我们来看一下
getPassword
真正作用:
public char[] getPassword()
Returns the text contained in this TextComponent.
返回此TextComponent中包含的文本。 If the underlying document is null, will give a NullPointerException.
如果基础文档为null,则将提供NullPointerException。 For stronger security, it is recommended that the returned character array be cleared after use by setting each character to zero.
为了提高安全性,建议在使用后通过将每个字符设置为零来清除返回的字符数组。
Returns: the text
返回:文本
As you can see, it returns your password in a char[]
and since this class doesn't override toString
your call of uPass.getPassword().toString()
results in something like: 如您所见,它以
char[]
返回您的密码,并且由于此类不会覆盖toString
您对uPass.getPassword().toString()
调用将产生uPass.getPassword().toString()
结果:
[C@1d44bcfa
[C @ 1d44bcfa
which is the result of calling Object#toString
. 这是调用
Object#toString
的结果。
The length of this String is 11 and therefore larger then 10 and your else if
block ( else if ((uPass.getPassword().toString()).length()>10)
) will be entered. 此字符串的长度为11,因此大于10,然后输入
else if
块( else if ((uPass.getPassword().toString()).length()>10)
))。
To fix that, call the String
constructor String(char[])
like: 要解决此问题,请像下面这样调用
String
构造函数String(char[])
:
String Pass1 = new String(uPass.getPassword());
Please use this just as a "quick fix" for your current problem and try to find a way to use the originally returned char[]
. 请将此作为对您当前问题的“快速修复”,并尝试找到一种方法来使用最初返回的
char[]
。 As mentioned by the quoted JavaDoc it is recommened the "clean" the char array after using it, so the password won't be stored there anymore. 正如引用的JavaDoc所提到的,建议在使用完字符数组后建议“清理”该字符数组,因此不再将密码存储在此处。 By creating a String from the array, using
new String(uPass.getPassword())
, you're creating another object in the heap which contains the password and which also needs to be removed from there. 通过使用
new String(uPass.getPassword())
从数组创建字符串,您将在堆中创建另一个对象,该对象包含密码,并且也需要从该对象中删除该对象。 So it would add more work for you. 因此,它将为您增加更多的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.