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;
Code java:
<
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!)
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 Java:
/**Code removed by moderator*/
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.
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
Re: Can Someone please help me with my password program?
Quote:
Originally Posted by
mkrage
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.
Re: Can Someone please help me with my password program?
Quote:
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.
Quote:
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.
Quote:
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.
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