Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Char Array and Char array to String Conversion

Tags:

java

In my program I am getting a string from Database result set and convert it to char array like this:

emp.nid = rs.getString("nid").toCharArray();

In this part there is no error. The String is successfully converted to char array. But I have another code like this:

nid_txt.setText(emp.nid.toString());

This prints some freaky text. Not the original. Why was this happens? Please help me.

like image 483
Jayanga Kaushalya Avatar asked Dec 04 '22 07:12

Jayanga Kaushalya


1 Answers

You're calling toString on a char[] - and that inherits the implementation from Object, so you get the char[].class name, @ and then the hash of the object. Instead, call the String(char[]) constructor:

nid_txt.setText(new String(emp.nid));
like image 63
Jon Skeet Avatar answered Dec 21 '22 11:12

Jon Skeet