Results 1 to 10 of 10

Thread: if statement

  1. #1
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default if statement

    When the if statement is true it displays the error message as well. It also displays the error message twice when the statement is not true. Any ideas on why and how to fix it?

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            String getPassword = password.getText();
            String getUsername = username.getText();
           
            for(int tel = 0; tel<data1.length; tel++)
            {
                if((getUsername.equals(data1[tel].getUsername()))&&(getPassword.equals(data1[tel].getPassword())))
                {   Aktiwiteitupdate up = new Aktiwiteitupdate();
                    up.welkomnam.setText(getUsername+"!");
                    this.setVisible(false);               
                }
                else
              {
                    JOptionPane.showMessageDialog(null, "Wrong username or password.","ERROR",JOptionPane.ERROR_MESSAGE);
              }
            }
    
        }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: if statement

    For each and every element from your 'data1' array it checks whether it is correct or incorrect; that's what you told it to do ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: if statement

    I changed it to this now and there's still something wrong.
    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            String getPassword = password.getText();
            String getUsername = username.getText();
            int gekry = 0;
            int tel = 0;
            while(gekry == 0)
            {
                if((getUsername.equals(data1[tel].getUsername()))&&(getPassword.equals(data1[tel].getPassword())))
                {   Aktiwiteitupdate up = new Aktiwiteitupdate();
                    tel = tel+1;
                    System.out.println(tel);
                    up.welkomnam.setText(getUsername+"!");
                    gekry = 1;
                    this.setVisible(false);
                }
                else
              {
                    JOptionPane.showMessageDialog(null, "Wrong username or password.","ERROR",JOptionPane.ERROR_MESSAGE);
              }
            }
    
        }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: if statement

    What does 'still something wrong' mean? Do daemons fly out of your nostrils?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: if statement


    Whether I log in with the right username and password or not it displays the error, and it won't go away.

  6. #6
    ics1010 is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: if statement

    Your logic is a bit fuzzy. You're checking a list of potential un/pw combos against a single input un/pw. You're set up now to display an error for each time it fails and since it fails to update any counters on that event, you're in an endless loop. If that's the way you need it, put your control loop back in and you need something like this...
    int match = 0;
    for(tel = 0; tel < data1.len; tel++) {
    if(data1[tel] == positive_match) {
    match = 1;
    break;
    }
    }
    if(match == 1) {
    // un/pw has matched so act accordingly
    }
    else {
    // un/pw failed all matches so act accordingly
    }
    Last edited by ics1010; 08-10-2012 at 02:43 PM.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: if statement

    You might need to give some more detail here.
    What is data1.
    What is the loop attempting to do?

    It looks to me like you're trying to find the username/password combination in that array?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: if statement

    Quote Originally Posted by ics1010 View Post
    Your logic is a bit fuzzy. You're checking a list of potential un/pw combos against a single input un/pw. You're set up now to display an error for each time it fails and since it fails to update any counters on that event, you're in an endless loop. If that's the way you need it, put your control loop back in and you need something like this...
    int match = 0;
    for(tel = 0; tel < data1.len; tel++) {
    if(data1[tel] == positive_match) {

    Just as I wanted to reply that I got it right, I saw your message :). Thanks, I did a similar thing to what you were suggesting. My code now looks like this:

    match = 1;
    break;
    }
    if(match == 1) {
    // un/pw has matched so act accordingly
    }
    else {
    // un/pw failed all matches so act accordingly
    }
    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            String getPassword = password.getText();
            String getUsername = username.getText();
            int verander = 0;
            
            for(int tel = 0; tel<data1.length; tel++)
            {
                if((getUsername.equals(data1[tel].getUsername()))&&(getPassword.equals(data1[tel].getPassword())))
                {   
                    verander = 1;
                                   
                }
            }
            if(verander == 1)
            {
                Aktiwiteitupdate up = new Aktiwiteitupdate();
                up.welkomnam.setText(getUsername+"!");
                this.setVisible(false);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Wrong username or password.","ERROR",JOptionPane.ERROR_MESSAGE);
            }
    
        }

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: if statement

    I'd split things up in two separate (and simpler methods): 1) a method that tries to find a username/password combination in that array; it could return the index in the array or -1 if nothing was found; 2) a method that takes the appropriate action depending on the supplied username/password combination. That untangles the control flow of the program ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: if statement

    Quote Originally Posted by JosAH View Post
    I'd split things up in two separate (and simpler methods): 1) a method that tries to find a username/password combination in that array; it could return the index in the array or -1 if nothing was found; 2) a method that takes the appropriate action depending on the supplied username/password combination. That untangles the control flow of the program ...

    kind regards,

    Jos
    thanks Jos, but I got it working (above). Ill remember your recommendations for future reference. Thanks again.

Similar Threads

  1. Help with IF statement
    By Rahim2312 in forum New To Java
    Replies: 16
    Last Post: 05-09-2012, 04:12 PM
  2. if statement
    By Exceedinglife in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 01:25 AM
  3. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  4. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 06:57 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •