Convert PinField.getPassword () output to string

if ("Submit".equals(cmd)) { //Process the password. String UserNameInput=UserName.getText(); ///////////////////////////////ERROR Pin when printed shows error///////////////////////////// char[] PinInput = PinField.getPassword(); String pinInput=PinInput.toString(); //for debugging , print PinInput , but it prints garbage System.out.println("Pin entered is "+PinInput); //pinInput has garabage instead of the Pin that was entered //so the function isPasswordCorrect fails to verify UserName & Pin if (isPasswordCorrect(UserNameInput,pinInput)) { //some tasks } } boolean isPasswordCorrect(String Username,String Pin) { //verify username & pin } 

I need to convert PinInput from an array of characters to String, so that I can use the isPasswordCorrect () function. When I use the toString () method, it produces a value for garbage, what do I need to do to convert the PinInput value to String?

+4
source share
2 answers

Look at the String API, there is a constructor that takes a char array.

+5
source

@camickr is right. This is not bullshit; pinField.getPassword().toString() shows the address of char[] , for example. [ C@fd54d6 .

Application: String.valueOf(array) or new String(array) converts the result of getPassword() to String , but this prevents security when returning a char[] . Try storing the value locally and following the recommendations of the API: "it is recommended that the returned array of characters be cleared after use by setting each character to zero."

+4
source

All Articles