Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Password Array

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Password Array

    My assignment is to create a password validation applet, that compares user input to a specified set of passwords stored in an array.
    For some reason, no matter what I type in, it gives me the error saying the password is incorrect. I'm not sure why this is, any help would be much appreciated.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*; 
     
    public class Password extends JApplet implements ActionListener
    {
    	protected String pass;
    	protected Container con = getContentPane();
    	protected Font fntHeading = new Font("Times Roman", Font.ITALIC, 20);
    	protected JLabel labelGreeting = new JLabel("Enter Password (and click OK):");
    	protected JTextField txtPassword = new JTextField("", 10);
    	protected JButton btnOk = new JButton("OK");
    	protected JTextField txtAccess = new JTextField("Access Granted");
    	protected JTextField txtDenied = new JTextField
                                          ("Access Denied; Please re-enter a valid password");
    	String [] Password = {"rosebud", "redrum", "jason", "surrender", "dorothy"};
    	public void init()
    	{
    		FlowLayout layout = new FlowLayout();
    		con.setLayout(layout);
    		labelGreeting.setFont(fntHeading);
    		con.add(labelGreeting);
    		con.add(txtPassword);
    		con.add(btnOk);
    		btnOk.addActionListener(this);		
    	}
    	public void actionPerformed(ActionEvent authenticate) 
    	{
    		pass = txtPassword.getText();
    		if (pass == Password[0] || pass == Password[1] || pass == Password[2] 
                                                              || pass == Password[3] || pass == Password[4])
    			con.add(txtAccess);
    		else
    			con.add(txtDenied);
    		con.validate();
    	}
    }

    Also, from the instructions, the user is supposed to also be able to press enter in order to access the actionevent..... however I couldn't really find anywhere that specified how to do such a thing, and nothing happens when I press enter, I assume it's because I haven't created an eventlistener for it. Thank you.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Password Array

    Don't use == when comparing Strings. The equals method just compares whether two variables contain the same instance. Use the equals() method to compare the actual contents instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password Array

    It's funny, I was thinking that about 6 hours ago and totally forgot that equals() method was a thing.
    Thank you much.

    Nothing on about adding an enter key actionlistener though? >.<

  4. #4

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password Array

    Thank you!
    I guess I was googling the wrong stuff.

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password Array

    how might one go about comparing strings with capitols to strings with non-capitols.

    For example according to the instructions the Rosebud or RoSeBuD or any variation there of should be able to grant access. Not just all lowercase rosebud.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Password Array

    Take a look at the String API: String (Java Platform SE 7 )
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Can Someone please help me with my password program?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2012, 11:16 AM
  2. [SOLVED] password denied
    By chronoz13 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 28th, 2010, 09:22 PM
  3. [SOLVED] Password screens
    By Dave in forum AWT / Java Swing
    Replies: 7
    Last Post: August 26th, 2009, 06:37 AM
  4. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM
  5. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM