Re: Encrypter not working
Hello maxattack!
You have some logic errors (probably by mistake).
Code :
if (jtfResult.getText().equals("d"));
.
.
.
This semicolon in the end of the statement doesn't make much sense. Take a look at the if else if statement.
Re: Encrypter not working
Just tried it, that didn't work either. I may be doing it wrong, but im pretty sure its correct
Re: Encrypter not working
Quote:
Originally Posted by
maxattack
Just tried it, that didn't work either. I may be doing it wrong, but im pretty sure its correct
Can you show the code you tried? Or better a sample of it that shows the problem and can be executed without errors?
Re: Encrypter not working
Thats the thing, there is no error. Eclipse doesn't report anything. Heres my code -
Code :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Encrypter extends JFrame implements ActionListener {
private static final long serialVersionUID = 5L;
private JButton jbtEncrypt;
private JButton jbtClear;
private JButton jbtDecrypt;
private JButton jbtClose;
private JTextField jtfResult;
private double Output;
private double Input;
String display = "";
public Encrypter() {
setBackground(Color.gray);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4, 3));
p1.add(jbtClear = new JButton("Clear"));
p1.add(jbtEncrypt = new JButton("Encrypt"));
p1.add(jbtDecrypt = new JButton("Decrypt"));
p1.add(jbtClose = new JButton("Close"));
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jtfResult = new JTextField(25));
jtfResult.setHorizontalAlignment(JTextField.CENTER);
jtfResult.setEditable(true);
JPanel p = new JPanel();
p.setLayout(new GridLayout());
p.add(p2, BorderLayout.NORTH);
p.add(p1, BorderLayout.SOUTH);
p.setBackground(Color.gray);
add(p);
jbtEncrypt.addActionListener(new ListenToEncrypt());
jbtClear.addActionListener(new ListenToClear());
jbtDecrypt.addActionListener(new ListenToClear());
jbtClose.addActionListener(new ListenToClose());
}
class ListenToClear implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfResult.setText("");
Output = 0;
}
}
class ListenToDecrypt implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfResult.setText("");
Output = 0;
}
}
class ListenToClose implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(ABORT);
}
}
class ListenToEncrypt implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfResult.setText(display + "" + "" + "");
if (jtfResult.getText().equals("a")); {
jtfResult.setText("K");
if (jtfResult.getText().equals("b"));
jtfResult.setText("eec");
if (jtfResult.getText().equals("c"));
jtfResult.setText("3");
if (jtfResult.getText().equals("d"));
jtfResult.setText("R");
if (jtfResult.getText().equals("e"));
jtfResult.setText("l");
if (jtfResult.getText().equals("f"));
jtfResult.setText("W");
if (jtfResult.getText().equals("g"));
jtfResult.setText("9gd");
if (jtfResult.getText().equals("h"));
jtfResult.setText("h");
if (jtfResult.getText().equals("i"));
jtfResult.setText("1");
if (jtfResult.getText().equals("j"));
jtfResult.setText("Lm");
if (jtfResult.getText().equals("k"));
jtfResult.setText("54");
if (jtfResult.getText().equals("l"));
jtfResult.setText("Oo");
if (jtfResult.getText().equals("m"));
jtfResult.setText("n");
if (jtfResult.getText().equals("n"));
jtfResult.setText("o");
if (jtfResult.getText().equals("p"));
jtfResult.setText("LrLs");
if (jtfResult.getText().equals("q"));
jtfResult.setText("lkjh");
if (jtfResult.getText().equals("r"));
jtfResult.setText("M");
if (jtfResult.getText().equals("s"));
jtfResult.setText("t");
if (jtfResult.getText().equals("t"));
jtfResult.setText("_3");
if (jtfResult.getText().equals("u"));
jtfResult.setText("u");
if (jtfResult.getText().equals("v"));
jtfResult.setText("mnb");
if (jtfResult.getText().equals("w"));
jtfResult.setText("w__");
if (jtfResult.getText().equals("x"));
jtfResult.setText("yak");
if (jtfResult.getText().equals("y"));
jtfResult.setText("erf");
if (jtfResult.getText().equals("z"));
jtfResult.setText("E");
}
}
}
public static void main(String[] args) {
Encrypter enc = new Encrypter();
enc.pack();
enc.setLocationRelativeTo(null);
enc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
enc.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Re: Encrypter not working
I can see two problems in your code. Firstly, you didn't follow my hint in post #2. Your if statements are wrong. They don't have an error but the semicolon in the end of them is a logic error. Try implementing an if else if structure following the link I provided. After doing that try printing (or better use your debugger to see) the value of jtfResult.getText().
Re: Encrypter not working
Quote:
Originally Posted by
andreas90
I can see two problems in your code. Firstly, you didn't follow my hint in post #2. Your if statements are wrong. They don't have an error but the semicolon in the end of them is a logic error. Try implementing an if else if structure following the link I provided. After doing that try printing (or better use your debugger to see) the value of jtfResult.getText().
Ah, the semicolons were what caused the if else statements.
Now, it just clears the result.
Code :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Encrypter extends JFrame implements ActionListener {
private static final long serialVersionUID = 5L;
private JButton jbtEncrypt;
private JButton jbtClear;
private JButton jbtDecrypt;
private JButton jbtClose;
private JTextField jtfResult;
private double Output;
private double Input;
String display = "";
public Encrypter() {
setBackground(Color.gray);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4, 3));
p1.add(jbtClear = new JButton("Clear"));
p1.add(jbtEncrypt = new JButton("Encrypt"));
p1.add(jbtDecrypt = new JButton("Decrypt"));
p1.add(jbtClose = new JButton("Close"));
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jtfResult = new JTextField(25));
jtfResult.setHorizontalAlignment(JTextField.CENTER);
jtfResult.setEditable(true);
JPanel p = new JPanel();
p.setLayout(new GridLayout());
p.add(p2, BorderLayout.NORTH);
p.add(p1, BorderLayout.SOUTH);
p.setBackground(Color.gray);
add(p);
jbtEncrypt.addActionListener(new ListenToEncrypt());
jbtClear.addActionListener(new ListenToClear());
jbtDecrypt.addActionListener(new ListenToClear());
jbtClose.addActionListener(new ListenToClose());
}
class ListenToClear implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfResult.setText("");
Output = 0;
}
}
class ListenToDecrypt implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfResult.setText("");
Output = 0;
}
}
class ListenToClose implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(ABORT);
}
}
class ListenToEncrypt implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfResult.setText(display + "" + "" + "");
if (jtfResult.getText().equals("a")) {
jtfResult.setText("K"); }
else if (jtfResult.getText().equals("b"))
jtfResult.setText("eec");
else if (jtfResult.getText().equals("c"))
jtfResult.setText("3");
else if (jtfResult.getText().equals("d"))
jtfResult.setText("R");
else if (jtfResult.getText().equals("e"))
jtfResult.setText("l");
else if (jtfResult.getText().equals("f"))
jtfResult.setText("W");
else if (jtfResult.getText().equals("g"))
jtfResult.setText("9gd");
else if (jtfResult.getText().equals("h"))
jtfResult.setText("h");
else if (jtfResult.getText().equals("i"))
jtfResult.setText("1");
else if (jtfResult.getText().equals("j"))
jtfResult.setText("Lm");
else if (jtfResult.getText().equals("k"))
jtfResult.setText("54");
else if (jtfResult.getText().equals("l"))
jtfResult.setText("Oo");
else if (jtfResult.getText().equals("m"))
jtfResult.setText("n");
else if (jtfResult.getText().equals("n"))
jtfResult.setText("o");
else if (jtfResult.getText().equals("p"))
jtfResult.setText("LrLs");
else if (jtfResult.getText().equals("q"))
jtfResult.setText("lkjh");
else if (jtfResult.getText().equals("r"))
jtfResult.setText("M");
else if (jtfResult.getText().equals("s"))
jtfResult.setText("t");
else if (jtfResult.getText().equals("t"))
jtfResult.setText("_3");
else if (jtfResult.getText().equals("u"))
jtfResult.setText("u");
else if (jtfResult.getText().equals("v"))
jtfResult.setText("mnb");
else if (jtfResult.getText().equals("w"))
jtfResult.setText("w__");
else if (jtfResult.getText().equals("x"))
jtfResult.setText("yak");
else if (jtfResult.getText().equals("y"))
jtfResult.setText("erf");
else if (jtfResult.getText().equals("z"))
jtfResult.setText("E");
}
}
public static void main(String[] args) {
Encrypter enc = new Encrypter();
enc.pack();
enc.setLocationRelativeTo(null);
enc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
enc.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Re: Encrypter not working
Did you print what is being tested (ie jtfResult.getText())?
What is the purpose of the statement
Code :
jtfResult.setText(display + "" + "" + "");
Why don't you test directly the textfield's text?
Re: Encrypter not working
Quote:
Originally Posted by
andreas90
Did you print what is being tested (ie
jtfResult.getText())?
What is the purpose of the statement
Code :
jtfResult.setText(display + "" + "" + "");
Why don't you test directly the textfield's text?
jtfResult.getText() just locates the text, I have it set to 'setText' to set the 'encrypted' word in its place.
Re: Encrypter not working
Quote:
Originally Posted by
maxattack
jtfResult.getText() just locates the text, I have it set to 'setText' to set the 'encrypted' word in its place.
The "encrypted" word is set inside every if statement. I talk about the statement
Code :
jtfResult.setText(display + "" + "" + "");
which is BEFORE the if statements.
The way it is your code now when the user presses the "Encryption" button, it first sets the text to something else than the word the user inputted with the above statement and then goes to the if statements. Can you see what's going on?
As I mentioned before print out jtfResult.getText()and you 'll see why your code is not working as intended.
Re: Encrypter not working
Oh, thanks. It's almost working perfectly, but I still need to make it register words. Should I do an array, or is there a different way? Thanks
Re: Encrypter not working
Quote:
Originally Posted by
maxattack
Oh, thanks. It's almost working perfectly, but I still need to make it register words. Should I do an array, or is there a different way? Thanks
Yoe are welcome. I would try a HashMap with the regular words as keys and the encrypted ones as values. Read the link for further information.
Re: Encrypter not working
Any idea of how to make it work with multiple letters together? The HashMap failed.
thanks
Re: Encrypter not working
Quote:
Originally Posted by
maxattack
Any idea of how to make it work with multiple letters together? The HashMap failed.
A HashMap would work. If it's not working for you, we won't be able to help figure out why if you don't show us and tell us.
Re: Encrypter not working
It just gives me errors,
Code :
HashMap hm = new HashMap();
hm.put("a", new Double(K));
hm.put("b", new Double(eec));
hm.put("c", new Double(s));
hm.put("d", new Double(Ab));
thats what I have at the beginning, and it doesn't work.
thanks
Re: Encrypter not working
Quote:
Originally Posted by
maxattack
It just gives me errors,
That doesn't mean that the technique doesn't work but rather that you're using it wrong. You should look at the error messages and try to figure out what they mean.
Quote:
Code :
HashMap hm = new HashMap();
hm.put("a", new Double(K));
hm.put("b", new Double(eec));
hm.put("c", new Double(s));
hm.put("d", new Double(Ab));
thats what I have at the beginning, and it doesn't work.
What are you trying to do with new Double(K)? That doesn't make sense to me.