Java Converting: char[] array --> String

char, java, string

Solution

Use the `String(char[])` constructor.

char [] password = c.readPassword("Enter your password: ");
String stringPassword = new String(password);

And when you compare, don't use `==`, use `.equals():

if(stringPassword.equals("Password")){

Problem

How do you convert a character array to a String? I have this code ``` Console c = System.console(); if (c == null) { System.err.println("No console."); System.exit(1); } char [] password = c.readPassword("Enter your password: "); ``` I need to convert that to a String so I can verify ``` if(stringPassword == "Password"){ System.out.println("Valid"); } ``` Can anyone help me with this?

Original source

Related problems