Showing multiple information messages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    Showing multiple information messages

    Hello world.

    I'm trying to teach myself and I'm having trouble with this simple codes. I just want to show if the user typed the username and the password correctly, Now the problem goes like this, it displays multiple information messages if both the username and the password are incorrect or if they are both null. And also, at line 36 I'm having trouble too. Please guide me : )

    [CODE=java]String UserName = "reysean";
    String Password = "lotus18";
    boolean CheckUserName=f alse;
    boolean CheckPassword=f alse;

    //Check for username
    if (txtUserName.ge tText().isEmpty ()){
    JOptionPane.sho wMessageDialog( null,"Please enter username.");
    CheckUserName=f alse;
    txtUserName.gra bFocus();
    }
    else if (txtUserName.ge tText().equals( UserName)) {
    CheckUserName=t rue;
    }
    else{
    JOptionPane.sho wMessageDialog( null,"Please enter correct username.");
    CheckUserName=f alse;
    txtUserName.gra bFocus();
    }

    //Check for password
    if (txtPassword.eq uals("")){
    JOptionPane.sho wMessageDialog( null,"Please enter password.");
    CheckPassword=f alse;
    txtPassword.gra bFocus();
    }
    else if (txtPassword.eq uals(Password)) {
    CheckPassword=t rue;
    }
    else{
    JOptionPane.sho wMessageDialog( null,"Please enter correct password.");
    CheckPassword=f alse;
    txtPassword.gra bFocus();
    }

    if ((CheckUserName =true) && (CheckPassword= true)){
    JOptionPane.sho wMessageDialog( null, "The username is: " + UserName + "\n The password is: " + Password );
    }[/CODE]

    Rey Sean
    Last edited by lotus18; Mar 27 '08, 01:54 PM. Reason: append multiple
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What is the purpose of the final else blocks in your chains of if-else?
    e.g
    [CODE=java] ...else{
    JOptionPane.sho wMessageDialog( null,"Please enter correct username.");
    CheckUserName=f alse;
    txtUserName.gra bFocus();
    }[/CODE]
    Is already handled by the first if part. That's why you have many messages.

    For the final part, realize that
    1.) == is not the same as =
    2.) if you have a boolean variable called test, then
    if(test == true) is the same as if(test)

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      In VB, I can use Exit Sub for this after prompting a messages (It will exit the next line). Is there any alternative for this?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by lotus18
        In VB, I can use Exit Sub for this after prompting a messages (It will exit the next line). Is there any alternative for this?
        Yes, in grown up normal programming languages we use 'return'.

        kind regards,

        Jos ;-)

        Comment

        • lotus18
          Contributor
          • Nov 2007
          • 865

          #5
          Originally posted by JosAH
          Yes, in grown up normal programming languages we use 'return'.

          kind regards,

          Jos ;-)
          Oh I see. I've forgot to use return. Thanks JosAH.

          Rey Sean

          Comment

          • lotus18
            Contributor
            • Nov 2007
            • 865

            #6
            Ahh.. Sorry guys, i can't still make it. I can't see this message even if both the username and password are correct.:

            [CODE=java]JOptionPane.sho wMessageDialog( null, "The username is: " + UserName + "\n The password is: " + Password );[/CODE]

            Any ideas?

            What I did was (As r035198x's suggestion at post #2).

            [CODE=java] if ((CheckUserName ) && (CheckPassword) ){
            JOptionPane.sho wMessageDialog( null, "The username is: " + UserName + "\n The password is: " + Password );
            }[/CODE]

            Rey Sean
            Last edited by lotus18; Mar 28 '08, 11:45 AM. Reason: deleted img[]

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Post the code that you have now.

              Comment

              • lotus18
                Contributor
                • Nov 2007
                • 865

                #8
                This is my code:

                [CODE=java]String UserName = "reysean";
                String Password = "lotus18";
                boolean CheckUserName=f alse;
                boolean CheckPassword=f alse;

                //Check for username
                if (txtUserName.ge tText().isEmpty ()){
                JOptionPane.sho wMessageDialog( null,"Please enter username.");
                CheckUserName=f alse;
                txtUserName.gra bFocus();
                return;
                }
                else if (txtUserName.ge tText().equals( UserName)) {
                CheckUserName=t rue;
                //goto CheckPassword;
                //txtUserName.gra bFocus();
                }
                else{
                JOptionPane.sho wMessageDialog( null,"Please enter correct username.");
                CheckUserName=f alse;
                txtUserName.gra bFocus();
                return;
                }

                //Check for password
                if (txtPassword.eq uals("")){
                JOptionPane.sho wMessageDialog( null,"Please enter password.");
                CheckPassword=f alse;
                txtPassword.gra bFocus();
                return;
                }
                else if (txtPassword.ge tPassword().equ als(Password)) {
                CheckPassword=t rue;
                }
                else{
                JOptionPane.sho wMessageDialog( null,"Please enter correct password.");
                CheckPassword=f alse;
                txtPassword.gra bFocus();
                txtPassword.sel ectAll();
                return;
                }

                if ((CheckUserName ) && (CheckPassword) ){
                JOptionPane.sho wMessageDialog( null, "The username is: " + UserName + "\n The password is: " + Password );
                }[/CODE]

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Why are people so afraid to create a few small methods? Your logic boils down
                  to the simple (pseudo code):

                  Code:
                  if (!(username.equals(inputUsername()) & password.equals(inputPassword())))
                     showIncorrectInput();
                  else
                     showCorrectInput();
                  Note that you should never implicitly tell a cracker that something is correct, so
                  the showIncorrectIn put() simply shows that something was incorrect. Also note
                  that I used a single & instead of a double && so that no matter whether the
                  username was incorrect, the program still asks for a password.

                  Now you need to implement the following simple methods:

                  - inputUsername()
                  - inputPassword()
                  - showIncorrectIn put()
                  - showCorrectInpu t()

                  I don't consider their implementation rocket science.

                  kind regards,

                  Jos

                  Comment

                  • lotus18
                    Contributor
                    • Nov 2007
                    • 865

                    #10
                    Sorry for the late reply. hehe... We had just arrived from our short vacation. I did it on that way to show me how that simple program flows. Yes, you are right, the crackers must not have a hint to crack usernames and passwords. Thanks for guiding me Joash.

                    Rey Sean

                    Comment

                    Working...