Forum Stats

  • 3,875,869 Users
  • 2,266,977 Discussions
  • 7,912,360 Comments

Discussions

re: username and password

843807
843807 Member Posts: 46,582
edited Jun 19, 2010 10:29AM in Abstract Window Toolkit (AWT)
Used code tags this time. Ya so i was wondering about how to input a username and make it go together with a password.

my frame looks like

Username: ____
Password:______

so i have labels and text fields just need to know how to incorporate them.

I tried the same concept as the password by creating a boolean and setting the correct name but in the actionperformed when i .getText from user input it gives me an error. For the password .getPassword(im using a JPasswordField) it works. Any Ideas
public void actionPerformed( ActionEvent evt)
  {
      {
        String cmd = evt.getActionCommand();
        String text = evt.getActionCommand();
    if(name.equals(text))
    {
         //here is the error(menutextname is the JTextField)
        char[] input = menutextname.getText();
    }

    if (password.equals(cmd))
    { //Process the password.
        
        char[] input = menupasswordtext.getPassword();
        if (isPasswordCorrect(input))
        {
          options();
          menuframe.setVisible(false);
        }
        else {
            JOptionPane.showMessageDialog(menuframe,
                "Invalid password. Try again.",
                "Error Message",
                JOptionPane.ERROR_MESSAGE);
        }
     }
      }

      if(evt.getActionCommand().equals("New BS"))//if the action equals 'new game'
    {
        BS();
        optionframe.setVisible(false);
    }
  }
  
private static boolean isNameCorrect(char[] input)
  {
      boolean isCorrect= true;
      char[] correctname = {'b','o','b'};
      if (input.length!=correctname.length)
      {
          isCorrect=false;
      }
      else
      {
         isCorrect = Arrays.equals (input, correctname) ;
      }
      Arrays.fill(correctname,'0');
      return isCorrect;
  }
  
private static boolean isPasswordCorrect(char[] input)
  {
    boolean isCorrect = true;
    char[] correctPassword = { 'p', 'a', 's', 's', 'w', 'o', 'r','d' };
    if (input.length != correctPassword.length) {
        isCorrect = false;
    } else {
        isCorrect = Arrays.equals (input, correctPassword);
    }

    //Zero out the password.
    Arrays.fill(correctPassword,'0');

    return isCorrect;
  }

}
Edited by: babojee on Jun 9, 2010 7:42 PM

Edited by: babojee on Jun 9, 2010 8:30 PM

Edited by: babojee on Jun 9, 2010 8:35 PM
«1

Comments

  • 843807
    843807 Member Posts: 46,582
    ...in the actionperformed when i .getText from user input it gives me an error
    What does this mean? Error codes would be nice to have and a complete description would also be very helpful.
  • 843807
    843807 Member Posts: 46,582
    edited Jun 9, 2010 11:52PM
    this line:
    char[] input = menutextname.getText();
    gives me and error.

    basically what im trying to do is create a username and password

    if u have any other way of doing the username and password id like to hear it

    Edited by: babojee on Jun 10, 2010 3:51 AM
  • 843807
    843807 Member Posts: 46,582
    edited Jun 10, 2010 11:02AM
    char[] input = menutextname.getText();
    Just to finish out your idea...
    char[] input = menutextname.getText().toCharArray();
    Do you read the API? getText returns String you are asking for an array of chars, if you look in the String API, guess what: it shows toCharArray().
  • 843807
    843807 Member Posts: 46,582
    thank you, sorry for the trouble
  • 843807
    843807 Member Posts: 46,582
    No trouble, it's just intersting seeing how many people don't read the free documentation available.
  • 843807
    843807 Member Posts: 46,582
    edited Jun 10, 2010 8:57PM
    srry i never knew about it and im in school and my teacher is like here's a major project, oh and btw u have to learn GUI by yourself and create a program for a client. Useless teachers these days.

    one more thing about the username and password

    say it the user wants to change it

    so i have

    old password:

    new password:

    save(button)

    so i was thinking about it as getting the user input just the same as before when the user logins in and getting the users input for new password and then storing/saving into in the char passwordCorrect ( 's'). but the problem with me is i understand it but when i translate it into code it just comes out ................blah.

    Edited by: babojee on Jun 11, 2010 12:53 AM
  • darrylburke
    darrylburke Member Posts: 18,007
    babojee wrote:
    srry i never knew about it and im in school and my teacher is like here's a major project, oh and btw u have to learn GUI by yourself and create a program for a client. Useless teachers these days.
    Certainly your teachers didn't teach you that every sentence starts with a capital letter, or how to spell words like "sorry" "I'm" "you" or that the first person singular is written with a capital I.

    It's not your fault, your teachers are to be blamed... or not?

    db
  • 843807
    843807 Member Posts: 46,582
    lol nah he taught nothing at all.

    also i know how to spell its just when i type on a computer i use abbreviations.

    If I do formal typing I will type properly.
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    babojee wrote:
    lol nah he taught nothing at all.

    also i know how to spell its just when i type on a computer i use abbreviations.

    If I do formal typing I will type properly.
    Be warned that you are on the express train to 'being ignored for your laziness' if you do not take the time to type properly. If you cannot expend the effort to type properly, why should we expend the effort to wade through such dross?
  • 843807
    843807 Member Posts: 46,582
    OK, I'm sorry about that then I just assumed that everyone knows that people type with abbreviations on the internet. It isn't laziness, it is just the way of typing on the internet. If you want me to type properly to you I will then. Now back to my question you guys got any ideas?
This discussion has been closed.