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 12 of 12

Thread: Need help with checking passwords

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with checking passwords

    I made a program that checks a password that you enter. This is different than most because i want to enter it using buttons. If you get it right the program display "Correct" if you get it wrong it displays "Incorrect". The problem is when i run the program it always say the password is incorrect. Please help me fix this problem. You can find the problem in the enterListener new the bottom.
    Thanks.



            //============================================EnterListener
    		class EnterListener implements ActionListener {
    		public void actionPerformed(ActionEvent evt) {
    		Object  source = (JButton)evt.getSource();
     
    // The password is 8897		
    // Here is the problem
     
                   if (source.equals(buttonOrder)) {
    			_displayField.setText("Correct");
     
    		} else { 
    			_displayField.setText("Incorrect, try again");
    			}
     
     
    		}
     
     
     
    		}


    Here is the full program.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    class Password2 extends JFrame {
    //===========================================
    private static final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20);
     
    //===================================================
    private JTextField _displayField;
     
    private boolean _startNumber = true;
    private boolean code = true;
     
    //================================================
      public static void main(String[] args) {
     
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception unused) {
                ; 
            }
     
     
            Password2 window = new Password2();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
      }
     
     
    //===========================================================
     
    public Password2() {
    _displayField = new JTextField("Enter Password", 12);
    _displayField.setHorizontalAlignment(JTextField.RIGHT);
    _displayField.setFont(BIGGER_FONT);
    ActionListener enterListener = new EnterListener();
    JButton clearButton = new JButton("Clear");
            clearButton.setFont(BIGGER_FONT);
            clearButton.addActionListener(new ClearListener());
    JButton enterButton = new JButton("Enter");
     	enterButton.setFont(BIGGER_FONT);
            enterButton.addActionListener(enterListener);
            ActionListener numListener = new NumListener();
    String buttonOrder = "123456789 0 ";
    JPanel buttonPanel = new buttonPanel();
    buttonPanel.setLayout(new GridLayout(5, 3, 2, 2));
    for (int i = 0; i <buttonOrder.length(); i++) {
    	String keyTop = buttonOrder.substring(i, i+1);
    	JButton b = new JButton(keyTop);
    	if (keyTop.equals(" ")) {
    		b.setEnabled(false);
    	} else {
    		b.addActionListener(numListener);
    		b.setFont(BIGGER_FONT);
    	}
    	buttonPanel.add(b);
    }
     
    JPanel clearPanel = new clearPanel();
    clearPanel.setLayout(new FlowLayout());
    clearPanel.add(clearButton);
    clearPanel.add(enterButton);
     
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout(5, 5));
    content.add(_displayField,	BorderLayout.NORTH );
    content.add(buttonPanel	, BorderLayout.CENTER);
    content.add(clearPanel	,BorderLayout.SOUTH );
     
    content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
     
     
      this.setContentPane(content);
            this.pack();
            this.setTitle("Password");
            this.setResizable(false);
            this.setLocationRelativeTo(null);	
     
    }
    	//================================================
    	class buttonPanel extends JPanel {
    	public void paintComponent(Graphics comp) {
    		super.paintComponent(comp);
    		Graphics2D comp2D = (Graphics2D)comp;
    		comp2D.setColor(Color.green);
    		setBackground(Color.black);
    		int width = getSize().width;
    		int height = getSize().height;
    			Font currentFont = new Font("Serif" , Font.BOLD+Font.ITALIC, 36);
    			comp2D.setFont(currentFont);
    			comp2D.drawString("Password", width - 200, height - 5);
     
     
    	}	
     
     
    	}
    	//==========================================================
    	class clearPanel extends JPanel {
    	public void paintComponent(Graphics comp) {
    		super.paintComponent(comp);
    		Graphics2D comp2D = (Graphics2D)comp;
    		comp2D.setColor(Color.red);
    		setBackground(Color.blue);
     
    	}
    	}
     
    	//=========================================================
       private void actionClear() {
            _startNumber = true;         
            _displayField.setText("0");
       }
    	    //////////////////////////////////// inner listener class NumListener
     
    	    class NumListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                String digit = e.getActionCommand(); 
                if (_startNumber) {
                    _displayField.setText(digit);
                    _startNumber = false;
                } else {
                    _displayField.setText(_displayField.getText() + digit);
                }
            }
        }
     
     
            //============================================EnterListener
    		class EnterListener implements ActionListener {
    		public void actionPerformed(ActionEvent evt) {
    		Object  source = (JButton)evt.getSource();
     
    // The password is 8897		
    // Here is the problem
     
                   if (source.equals(buttonOrder)) {
    			_displayField.setText("Correct");
     
    		} else { 
    			_displayField.setText("Incorrect, try again");
    			}
     
     
    		}
     
     
     
    		}
     
     
    //////////////////////////////////// inner listener class ClearListener
        class ClearListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                actionClear();
            }
        }
    }
    Last edited by kb1213; April 11th, 2011 at 09:17 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with checking passwords

    == != equals()....see http://www.javaprogrammingforums.com...html#post18725

    Edit: Please don't post the same question twice to the forums. I've removed your other post and moved this to the appropriate forum
    Last edited by copeg; April 1st, 2011 at 05:01 PM.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with checking passwords

    Thanks. But it still doesn't work.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with checking passwords

    Object  source = (JButton)evt.getSource();
    if (source.equals("8897")) {
    You are comparing the button that caused the event to the String. Obviously this will never be true.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with checking passwords

    Convert your object value into string and try comparing with that. I agree with Junky's answer as well.

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with checking passwords

    I'm not sure I know what your saying. I figured out that the buttons that I put in have no purpose because I have the String buttonOrder. What I don't know is how to get a source from the string. String buttonOrder is a string of numbers that are used to label the buttons on this grid layout.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with checking passwords

    As I said you are comparing the button with the String. Perhaps you should get the String that is displayed on the button and compare that instead.

  8. #8
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with checking passwords

    When I get the String from the button, it says that it cannot find the symbol. Please help me with this. I don't know what else to do.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with checking passwords

    We don't read minds. Post your code. If it is too long then create a SSCCE.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with checking passwords

    Oh and the exact error message

  11. #11
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with checking passwords

    I already posted the whole code and here is the error message. It should be pointing to buttonOrder but I couldn't figure out to format it.



    Password2.java:129: cannot find symbol
    symbol : variable buttonOrder
    location: class Password2.EnterListener
    if (source.equals(buttonOrder)) {
    ^
    1 error
    Process javac exited with code 1

  12. #12
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with checking passwords

    Never mind. Thank you everyone. I finally figured out the problem.

Similar Threads

  1. Checkform - Checking 2 fields
    By christh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 06:55 AM
  2. efficient way of checking duplicates
    By starmandell in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 2nd, 2011, 06:52 PM
  3. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  4. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM

Tags for this Thread