I'm creating an application in java, which, while creating a new account, has to ask the user to enter password & username, if the username or password is missing it sends the appropriate warnings to the user, else, if nor username or password is missing it sends the data to a database I've created in SQL. Knowing that JPasswordField.getPassword() returns an array of chars, I've tried** to get the user password this way:
private static void doIfSomethingIsMissing() {
// Turning JPasswordField to a String.
String passwordText = new String(passwordField.getPassword());
if(passwordText.toString().equals("")) {
accountCreationWarning.setText(missingPassword);
}
else if(usernameField.getText().toString().equals("")) {
accountCreationWarning.setText(missingUser);
}
}
This is what it does:
- If you have inserted neither username nor password, it says "password is missing" (rightly).
- If you have only inserted password it says "password is missing" (rightly)
- If you have only inserted username it (tremendously wrongly) updates the database to insert the new user.
** Of course this is not the only thing I've tried, I've tried tons of solutions, so if you think I haven't made enough research, you are wrong.