This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes casting an array of chars to a String??plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "casting an array of chars to a String??plz help" Watch "casting an array of chars to a String??plz help" New topic
Author

casting an array of chars to a String??plz help

Aboo Bolaky
Greenhorn

Joined: Apr 02, 2004
Posts: 19
hello...im building a SWING login interface with the JPasswordField Component. To obtain the unmasked password i use
char password[]=txtPassword.getPassword();

the function getPassword returns an array of chars...but i need the result as simply of type String.
i tried the type casting several times but im not having the hang of it...can anyone help me plz?
thanks in advance,
Drftwy
Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
String class has a constructor that takes in char[]:
public String(char[] value);

simply do:

char[] pwdArrray = ...
String pwdStr = new String(pwdArrray);
Aboo Bolaky
Greenhorn

Joined: Apr 02, 2004
Posts: 19
thnks..
didnt think it would be that easy...was struggling with type casting!!..
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Considering object references, note that you cannot change the type of an object with a cast. A cast creates a reference of a different type than the reference being cast, but both references would point to the same, unchanged object.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting an array of chars to a String??plz help
 
Similar Threads
please correct the code
I enter a password into a JPasswordField, but nothing happens??
convert an array of String to char
The method setAlt(String) in the type BaseHandlerTag is not applicable for the arguments (int)
Reading and converting only one line to String and storing it in an array