rubin.kazan
rubin.kazan

Reputation: 131

Else statement not recognised java

 private void EnterbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                            

    if (Usernamefield.getText().equalsIgnoreCase("User"));
    if (Passwordfield.getText().equalsIgnoreCase("420"));
{
    JOptionPane.showMessageDialog(null, "Welcome, User!");   
}                                           
else 
{

    JOptionPane.showMessageDialog(null, "Wrong Username/Password!");

}

It says that an else statement cannot work without an if, there are clearly 2 if statements however. I would like to create a simple log-in GUI in Java.

Upvotes: 0

Views: 96

Answers (2)

Derek MC
Derek MC

Reputation: 376

Try the below code.

 private void EnterbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                           

     if (Usernamefield.getText().equalsIgnoreCase("User")  && Passwordfield.getText().equalsIgnoreCase("420"))
     {
        JOptionPane.showMessageDialog(null, "Welcome, User!");   
     }                                           
     else 
     {    
        JOptionPane.showMessageDialog(null, "Wrong Username/Password!");    
     }

Upvotes: 1

Eran
Eran

Reputation: 393811

change

    if (Usernamefield.getText().equalsIgnoreCase("User"));
    if (Passwordfield.getText().equalsIgnoreCase("420"));

to

if (Usernamefield.getText().equalsIgnoreCase("User") && 
    Passwordfield.getText().equalsIgnoreCase("420"))
{
    JOptionPane.showMessageDialog(null, "Welcome, User!");   
}                                           
else 
{
    JOptionPane.showMessageDialog(null, "Wrong Username/Password!");
}

You want both conditions to be true in order to accept the user.
And don't put a ; at the end of an if condition, since that makes it an empty if statement that does nothing (that's the reason your else had no matching if).

Upvotes: 7

Related Questions