jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char to String" Watch "char to String" New topic

char to String

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
I wanted to ask a simple question. I am implementing a keylistener on a
textfield. As soon as a user types a character, it's clone is printed on
another textfield. For this I am using getKeyChar() method on the KeyEvent
object. It returns a char type on every key stroke. Now, how do I change this
char data type to a string type and show in the latter textfield. So the
actual thing to do is :
I type on TextField1 : abcdef
The System shows on TextField2 : abcdef
Finally, Swing's JPasswordField also has a method getPassword() to return a
char based (value or array). How do I convert it to a string and store in the
database or in TextField or in a physical file.
So Please help!!!
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
sachin patel
Ranch Hand

Joined: Nov 28, 2000
Posts: 75
well you can convert the char arrray returned by getPassword like this

String s = new String(password.getPassword());
where password is JPasswordField...
this works because String class has constructor that takes char array as argument...
hope that helps

Learn from others mistakes. Life is too short to make all yourself.

Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

for your first question you can do this:
Character c = new Character(e.getKeyChar());
String s = c.toString();

Visit my download page
I agree. Here's the link: http://aspose.com/file-tools
subject: char to String
It's not a secret anymore!