Mr.Maze Mr.Maze - 1 year ago 80
Java Question

Concatenate String and variable in JOptionPane window

I have the following method in a test environment, I'm trying to attach the variable count "

to the
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);

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download