2012-05-02 69 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)); 

哪位能適合您的需求。

相關問題