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: Can Someone please help me with my password program?

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

    Default Can Someone please help me with my password program?

    I am trying to make a program in its own JFrame that has a JPasswordField that you type in something and it checks to see if its the password. Also there are two hint buttons next to the password field that gives you hints. When i ran my program everything worked fine except when i actually typed in the right password it wouldn't work! I have no idea what i did wrong and this is what I typed(not including the main method because that is in another class):
    package PasswordGame;
    <
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPasswordField;
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Crap extends JFrame {
    	private JPasswordField Pass;
    	private JLabel Label;
    	private JButton Hint1;
    	private JButton Hint2;
    		public Crap() {
    			super("Hello");
    			setLayout( new FlowLayout());
     
    			Label= new JLabel("Guess the password or click the button for a hint!");
    			add(Label);
     
    			Pass = new JPasswordField("                 ");
    			add(Pass);
     
    			Hint1 = new JButton("Hint 1");
    			Hint2 = new JButton("Hint 2");
    			add(Hint1);
    			add(Hint2);
     
    			thehandler handler = new thehandler();
    			Hint1.addActionListener(handler);
    			Pass.addActionListener(handler);
    			Hint2.addActionListener(handler);
    		}
     
    		private class thehandler implements ActionListener {
    			public void actionPerformed(ActionEvent event) {
    				String string= "";
    				String abc= "abc";
    				String ABC= "ABC";
    				if(event.getSource()==Pass) {
    					string=String.format(event.getActionCommand());
    					if(string==abc) {
    						JOptionPane.showMessageDialog(null, "YOU GOT IT RIGHT! YAY!");
    					}
    					else if(string==ABC) {
    						JOptionPane.showMessageDialog(null, "YOU GOT IT RIGHT! YAY!");
    					}
    					else {
    						JOptionPane.showMessageDialog(null, "Your wrong, try again! :P");
    					}
     
    				}
    				else if(event.getSource()==Hint1) {
    					JOptionPane.showMessageDialog(null, "Hint 1: It is three letters...");
     
    				}
    				else if(event.getSource()==Hint2) {
    					JOptionPane.showMessageDialog(null, "Hint 2: Michael Jackson sings this in a song...");
     
    				}
     
    				}
    		}
     
    }>



    So I have no idea why when it checks if string is equal to abc it screws up so if someone could please help me it would be much appreciated! Then again thanks if you actually bothered to read and try to solve it. If you need to ask me any questions just comment. (or there is a typo!)
    Last edited by mkrage; October 14th, 2012 at 08:01 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can Someone please help me with my password program?

    hi mkrage,
    I checked your code and whats going wrong is :

    1. you comparing the string variable value with abc variable value using ==,actually you should use .equals() method.
    2. also you should trim the input value before compare since you are getting the space with which you have initialized your password field.
    3.Also, the password field's value should be reset each time.

    so by doing all above changes, the code will look like this (changed code is written in bold type) :

    /**Code removed by moderator*/
    Last edited by jps; October 15th, 2012 at 01:27 PM. Reason: spoonfeeding

  3. The Following User Says Thank You to ind For This Useful Post:

    mkrage (October 16th, 2012)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can Someone please help me with my password program?

    @ind
    Although everyone appreciates your reply to their questions, posting code or corrected code is frowned upon.
    Please see: The problem with spoonfeeding.

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can Someone please help me with my password program?

    Dear jps,
    What is wrong with correcting code? He is clearly just trying to help me and i appreciate that but now i can't see it cuz it was removed! I am not familiar with the .equals() method and do not know how it is set up or what parameters it takes! If he is just trying to help ne then i do not see why that is frowned upon. Please if I give permission for him to correct code then is it ok?

    Thanks for being a good moderator and following rules, but I do not see why that is not allowed.



    Also:
    Dear ind,
    Thank you alot for trying to help I really appreciate your help and it means alot to me.

    Thanks again all of you,
    Mkrage

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

    Default Re: Can Someone please help me with my password program?

    Quote Originally Posted by mkrage View Post
    I am not familiar with the .equals() method and do not know how it is set up or what parameters it takes!
    In that case you should consult the Java API and search the web for examples. The problem with spoonfeeding is that most of the time people do not learn how to solve the problem themselves and simply ask to be spoonfed again in the future.
    Improving the world one idiot at a time!

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can Someone please help me with my password program?

    What is wrong with correcting code?
    The general aim of the forum is to help you solve your problem, not to write your code. I understand how important it is to see sample code, but it is more important to be able to troubleshoot when there is a problem. The reason I say more important is because there will be a problem, and debugging is a major step in software development.



    I am not familiar with the .equals() method and do not know how it is set up or what parameters it takes!
    Which is exactly why mentioning it would lead you to your favorite search engine, from there to the API, and to some valuable experience in solving problems, as well as getting to know the documentation to the language you have set out to write programs in.



    Please if I give permission for him to correct code then is it ok?
    That would not be up to me, but my opinion would be "It is rude to ask for permission to break the guide lines in the first place." but ultimately it is up to the powers that be.



    Just to be clear, I believe code samples are a good thing, I post many samples in code. But there is a defined line between sample code and giving the working code as a reply.

  8. #7
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Can Someone please help me with my password program?

    just pass the given input string as the parameter when you use the .equals() method because Strings are objects and the == or != operators are used to compare primitive data

Similar Threads

  1. Check Password Program
    By m2msucks in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 6th, 2011, 01:39 AM
  2. Random Password
    By qwerty53 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 02:46 AM
  3. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 PM
  4. Password Program
    By computercoder in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 20th, 2010, 07:03 AM
  5. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM