将 PinField.getPassword() 的输出转换为字符串
if ("Submit".equals(cmd)) { //Process the password.
String UserNameInput=UserName.getText();
///////////////////////////////ERROR Pin when printed shows error/////////////////////////////
char[] PinInput = PinField.getPassword();
String pinInput=PinInput.toString();
//for debugging , print PinInput , but it prints garbage
System.out.println("Pin entered is "+PinInput);
//pinInput has garabage instead of the Pin that was entered
//so the function isPasswordCorrect fails to verify UserName & Pin
if (isPasswordCorrect(UserNameInput,pinInput))
{
//some tasks
}
}
boolean isPasswordCorrect(String Username,String Pin)
{
//verify username & pin
}
我需要将 PinInput 从字符数组转换为字符串,以便我可以使用函数 isPasswordCorrect() 。当我使用 toString() 方法时,它会产生垃圾值,我应该做什么来转换 PinInput 的值到 String ?
if ("Submit".equals(cmd)) { //Process the password.
String UserNameInput=UserName.getText();
///////////////////////////////ERROR Pin when printed shows error/////////////////////////////
char[] PinInput = PinField.getPassword();
String pinInput=PinInput.toString();
//for debugging , print PinInput , but it prints garbage
System.out.println("Pin entered is "+PinInput);
//pinInput has garabage instead of the Pin that was entered
//so the function isPasswordCorrect fails to verify UserName & Pin
if (isPasswordCorrect(UserNameInput,pinInput))
{
//some tasks
}
}
boolean isPasswordCorrect(String Username,String Pin)
{
//verify username & pin
}
I need to convert the PinInput from character array to String, so that I can use the function isPasswordCorrect() . When I use toString() method , it produces garbage value , what should I do to convert
the value of PinInput to String ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 String API,有一个接受 char 数组的构造函数。
Look at the String API, there is a constructor that accepts a char array.
@camickr 是对的。那不是垃圾;那是垃圾。
pinField.getPassword().toString()
显示char[]
的地址,例如[C@fd54d6
。附录:
String.valueOf(array)
或new String(array)
将转换getPassword()
到String
,但这会妨碍返回char[]
的安全优势。考虑将值保留在本地并遵循 API 指南:“建议在使用后通过将每个字符设置为零来清除返回的字符数组。”@camickr is right. That's not garbage;
pinField.getPassword().toString()
shows the address of thechar[]
, e.g.[C@fd54d6
.Addendum:
String.valueOf(array)
ornew String(array)
will convert the result ofgetPassword()
to aString
, but this thwarts the security benefit of returning achar[]
. Consider keeping the value local and following the API guidelines: "it is recommended that the returned character array be cleared after use by setting each character to zero."