-3

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:

  1. If you have inserted neither username nor password, it says "password is missing" (rightly).
  2. If you have only inserted password it says "password is missing" (rightly)
  3. 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.

  • 1
    Feeling a bit defensive, are we? Have you stepped through the code under the debugger? What is the value of "passwordText" when there's no password? Does it correctly execute "accountCreationWarning.setText()", and does the text display? What about "userNameField"? Why "else"? Why not just two, parallel "if" statements" Or change "else" to "or" (||)? Would you consider renaming the method, eg. `validateLogin()`? Why "toString()" of a String? What exactly is the problem? – paulsm4 Dec 08 '20 at 18:31
  • @paulsm4 The value of passwordText when there's no value is the default one. Answering to the second question, I've inserted the various output in the question, read it better. –  Dec 08 '20 at 18:44
  • 2
    Im not sure on the actual problem... but does this answer your question? https://stackoverflow.com/a/13461122/1133011 – David Kroukamp Dec 08 '20 at 19:16
  • 2
    (1-) *updates the database to insert the new user.* - how? There is no logic in this method to update the database. So your logic to determine when to update the database must be contained in some other method, so we don't have enough information to help. Post a proper [mre] demonstrating the problem. – camickr Dec 08 '20 at 19:33

2 Answers2

2

Suggested approach:

private boolean validateLogon () {
    if (passwordField.getPassword().length == 0) {
        accountCreationWarning.setText(missingPassword);
        return false;
    }
    if(usernameField.getText().equals("")) {
        accountCreationWarning.setText(missingUser);
        return false;
    }
    return true;
}
  1. The method is intended to validate that a username and password were both given. The name validateLogon() better expresses this.

  2. Changing from "static" allows you to access other members in your enclosing class. Including "accountCreationWarning". Which I'm guessing is probably a Swing JTextField (or equivalent). It also lets you declare "missingPassword" and "missingUser" as static final String constants in your class, if you wish.

  3. Returning "true" or "false" allows the caller to decide whether or not to update the database (and/or take other corrective action, if username/password validation failed).

  4. There's no need for extraneous "toString()" calls.

PS: This assumes "usernameField" and "passwordField" are both BLANK before the user enters anything.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

Not sure what was the actual question, but it seems you want to check for either userName or password is empty.

So remove else, and use separate if block to check userName is empty.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126