Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

re: username and password

843807Jun 9 2010 — edited Jun 19 2010
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 17 2010
Added on Jun 9 2010
11 comments
1,110 views