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

Thread: Encrypter not working

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Encrypter not working

    Hey, I need some help with an encrypter.

    I plan on actually making it use an encryption, but for now Im just having it set letters in the place of previous ones (and vice-versa).

    My code that actually contains the encryption is this -

    	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("d");
    			}
     
    			{
    				if (jtfResult.getText().equals("c"));
    				jtfResult.setText("3");
    			}
    			{
    				if (jtfResult.getText().equals("d"));
    				jtfResult.setText("R");
    			}
    			{
    				if (jtfResult.getText().equals("e"));
    				jtfResult.setText("l");
    			}
     
    		}
    	}


    no matter what I type in, it always gives me 'l', or whatever the last .setText that is registered. I will add every letter in the alphabet, but this is what I have so far.


    Thanks


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Encrypter not working

    Hello maxattack!

    You have some logic errors (probably by mistake).

    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.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Encrypter not working

    Just tried it, that didn't work either. I may be doing it wrong, but im pretty sure its correct

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by maxattack View Post
    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?

  5. The Following User Says Thank You to andreas90 For This Useful Post:

    maxattack (November 21st, 2012)

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Encrypter not working

    Thats the thing, there is no error. Eclipse doesn't report anything. Heres my 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
     
    	}
     
    }

  7. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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().
    Last edited by andreas90; November 21st, 2012 at 01:50 PM. Reason: typo

  8. The Following User Says Thank You to andreas90 For This Useful Post:

    maxattack (November 21st, 2012)

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by andreas90 View Post
    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.

     
    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
     
    }
     
    }

  10. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Encrypter not working

    Did you print what is being tested (ie jtfResult.getText())?
    What is the purpose of the statement
    jtfResult.setText(display + "" + "" + "");
    Why don't you test directly the textfield's text?

  11. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by andreas90 View Post
    Did you print what is being tested (ie jtfResult.getText())?
    What is the purpose of the statement
    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.

  12. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by maxattack View Post
    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
    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.

  13. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  14. #12
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by maxattack View Post
    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.

  15. The Following User Says Thank You to andreas90 For This Useful Post:

    maxattack (November 21st, 2012)

  16. #13
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Encrypter not working

    Any idea of how to make it work with multiple letters together? The HashMap failed.

    thanks

  17. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by maxattack View Post
    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.

  18. #15
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Encrypter not working

    It just gives me errors,

    	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

  19. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Encrypter not working

    Quote Originally Posted by maxattack View Post
    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.

    	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.

Similar Threads

  1. Something i've been working on.
    By jbrower95 in forum Java IDEs
    Replies: 0
    Last Post: July 17th, 2012, 04:45 PM
  2. NOT WORKING!!!!!
    By zezverige in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 19th, 2012, 10:30 AM
  3. Working with $
    By whome in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 11th, 2012, 03:00 PM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM