-2

I have the following method in a test environment, I'm trying to attach the variable count "loginAttempts" to the JOptionPane window so the user would know how many failed login attempts they've had so far. Essentially, I want to concatenate another string and a variable to this line if possible .

JOptionPane.showMessageDialog(null, "Enter a valid username and a password. You've had ||loginAttempts|| "failed login attempts", "Error", JOptionPane.ERROR_MESSAGE);


Here's the method out of the application" 

private void loginButton(java.awt.event.ActionEvent evt) {                             
    username = userName.getText();
    password = userPassword.getText();
    if (username.equals(finalusername) && password.equals(finalpassword) && loginAttempts <= 3) {
        loginSuccess = true;
    } else {
        loginSuccess = false;
        loginAttempts = loginAttempts + 1;
    }
    if (loginSuccess) {
        JOptionPane.showMessageDialog(null, "You have successfully logged in", "Success", JOptionPane.DEFAULT_OPTION);
    } else if (loginAttempts > 3) {
        JOptionPane.showMessageDialog(null, "You have had 3 failed login attempt, your account has been locked","Locked", JOptionPane.ERROR_MESSAGE);
    } else if (!loginSuccess) {
        JOptionPane.showMessageDialog(null, "Enter a valid username and a password", "Error", JOptionPane.ERROR_MESSAGE);
    }
} 
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Mr.Maze
  • 105
  • 1
  • 1
  • 8
  • 1
    it would be like this `JOptionPane.showMessageDialog(null, "Enter a valid username and a password , failed attempts " + loginAttempts , "Error", JOptionPane.ERROR_MESSAGE);` should check this http://stackoverflow.com/questions/3753869/how-do-i-concatenate-two-strings-in-java – Dev. Joel Oct 14 '16 at 15:03
  • So where is the problem? Do you not know how to concatenate strings in Java? (e.g.: using the + operator) Or what exactly are you looking for? – UnholySheep Oct 14 '16 at 15:03
  • I wasn't sure if concatenation works within showMessageDialog() Dev.Joel and L.Cabonne provided the solution. – Mr.Maze Oct 14 '16 at 15:08

1 Answers1

0

You can concatenate this way (replace the last else with that ) :

    } else if (!loginSuccess) {
        JOptionPane.showMessageDialog(null, "Enter a valid username and a password, Number of attempts : "+loginAttempts , "Error", JOptionPane.ERROR_MESSAGE);
    }
L. Carbonne
  • 471
  • 5
  • 10