2012-05-02 121 views
0

我正在写一个GUI程序,它有JPasswordField接收来自用户的文本。然后,我用这个如下这个值转换为字符串:为什么toString()为Java中的相同输入返回不同的值?

char[] pass = txtPass.getPassword(); //where txtPass is the JPasswordField 
System.out.println("Password:"); 
////////////////Check password///// 
for (int i=0; i< pass.length; i++) 
    System.out.println(pass[i]); 

System.out.println("String pass"+ pass.toString()); 

不过,凡事我执行应用程序,不同pass.toString的()我会收到。我希望他们是唯一的,以便我可以做更多的crytpgraphy功能。

回答

1

toString在阵列功能返回的数组中的字符。试试这个:

char[] pass = txtPass.getPassword(); //where txtPass is the JPasswordField 
System.out.println("Password:"); 
////////////////Check password///// 
for (int i=0; i< pass.length; i++) 
    System.out.println(pass[i]); 

System.out.println("String pass"+ new String(pass)); 

这将创建一个包含数组中字符的新字符串。

0

刚刚看过了Java SDK API,它规定得很清楚,

又见toString of JPasswordField

公共字符串的ToString(){返回的getClass()。的getName()+ “[” +中的paramString()+ “]”;}

toString方法返回整个Swing组件的toString,因此返回getClass()。getName()+ paramString()。而getPassword方法返回已输入组件密码字段的实际密码。

0

代替

的System.out.println( “字符串通” + pass.toString());

可以使用

System.out.println("String pass"+ String.valueOf(pass)); 

哪位能适合您的需求。

相关问题