bout JPasswordField. really need ur help?

ok guys. i hv this program to accept password from user. but now, i wanna change the password variable to bcme an array. meaning that i can stored more than one password. ive tried so many times but the result is still the same. many errors occurred. n this is the code. pls help me

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

public class Password extends Applet implements ActionListener

{

JPasswordField pass;

public void init ()

{

pass = new JPasswordField (5);

add (pass);

JButton done = new JButton ("Enter");

done.setActionCommand ("Enter");

done.addActionListener (this);

add (done);

}

public void actionPerformed (ActionEvent e)

{

if (pass.getText ().equals ("abcde12345"))

JOptionPane.showMessageDialog (null, "Access Granted", "Welcome", JOptionPane.INFORMATION_MESSAGE );

else

JOptionPane.showMessageDialog (null, "Unauthorized Access", "Incorrect Password", JOptionPane.ERROR_MESSAGE);

pass.setText ("");

}

}

2 Answers

Relevance
  • 9 years ago
    Favorite Answer

    Are you saying that you want multiple password input fields or you want to store multiple valid passwords?

    (1)

    class MyFrame extends JFrame {

    JPasswordField pFields[] = new JPasswordField[3];

    MyFrame()

    {

    super();

    for(JPasswordField pf: pFields)

    add(pf);

    }

    }

    (2)

    ArrayList<String> validPasswords = new ArrayList<String>();

    validPasswords.get("abc123"); validPasswords.get("def456"); validPasswords.get("ghi789");

    public void actionPerformed (ActionEvent e)

    {

    for(String validPassword: validPasswords) {

    if (pass.getText ().equals (validPassword))System.out.println("login success");

    }

    }

  • 9 years ago

    If we do this

    String myThing = "abc12345";

    String secretPass = pass.getText();

    even tho' the variable secretPass and the variable myThing have "abc12345" for their value .equals() will return false. The .equals() in this case will look like this to the JVM

    myString.equals( anotherString ) // or

    obj1.equals( obj2 ); // and remember an Object has a location in memory, not a value

    that being said...

    public void actionPerformed( ActionEvent e ) {

    if( e.getActionCommand().equalsIgnoreCase( "Enter" ) ) {

    String input = pass.getText();

    for( int i = 0; i< bushelOfpasswurds.length; i++ ) {

    if( bushelOfpasswurds[ i ].equalsIgnoreCase( input ) {

    JOptionPane.showMessageDialog( null, "Hale yeah!");

    break;

    }

    doThaUdderThang();

    } else {

Still have questions? Get your answers by asking now.