0

I'm making an encryption program and for some reason the program completely freezes up when I press the button. I'm not sure what's wrong because I've made many simple GUIs before and I've never encountered this is issue. Here's the void for the button:

private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String origMessage = txtDInput.getText();
        String encMessage = "";
        String revMessage = "";
        String extraStg1 = "";
        String extraStg2 = "";
        char tempChar;
        char tempExtraChar;
        int tempAscii;
        int tempExtraAscii;
        
        for (int i = origMessage.length() - 1; i >= 0; i = i--) //reverses message
        {
            revMessage = revMessage + origMessage.charAt(i);
        }
        
        for (int i = 0; i < revMessage.length(); i = i++)
        {
            tempChar = revMessage.charAt(i); //stores character in the tempChar variable
            tempAscii = (int)tempChar; //converts the character into an Ascii value
            tempAscii = tempAscii + 3; //adds 3 to Ascii value
            tempChar = (char)tempAscii; //converts Ascii value back into a character value
            encMessage = encMessage + tempChar; //adds the new character to the encrypted string and repeats for every character
        }
        
        for (int i = 0; i <= 7; i++)
        {
            tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
            tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
            extraStg1 = extraStg1 + tempExtraChar; //add the extra character to tempExtraStg1
        }
        
        for (int i = 0; i <= 7; i++)
        {
            tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
            tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
            extraStg2 = extraStg2 + tempExtraChar; //add the extra character to tempExtraStg2
        }
        
        encMessage = extraStg1 + encMessage + extraStg2;
        
        txtEncrypted.setText(encMessage);
    } 

I'm a beginner at this so I'd appreciate it if the answer are as simple as possible. Thanks.

2
  • My magic wand says you have an infinite loop in your program.
    – user253751
    Aug 31, 2020 at 17:16
  • Why are you doing this? for (int i = origMessage.length() - 1; i >= 0; i = i--) infinite loop.
    – aironman
    Aug 31, 2020 at 18:59

1 Answer 1

2

This is the problem:

for (int i = 0; i < revMessage.length(); i = i++)

The i = i++ is a no-op - it increments i, but then sets it back to the original value, so your loop will execute forever. Just change that to:

for (int i = 0; i < revMessage.length(); i++)

You actually have the same problem earlier:

for (int i = origMessage.length() - 1; i >= 0; i = i--)

should be

for (int i = origMessage.length() - 1; i >= 0; i--)

(As a side note, this isn't really "encryption" in a useful way, and you shouldn't roll your own encryption anyway, but I've addressed the question as asked.)

2
  • Ah... that's it. It works now. What a stupid mistake especially cause I did it the right way in other sections of the code. Thanks so much for your help.
    – Anth Math
    Aug 31, 2020 at 17:15
  • Yeah I know it's not particularly useful, but the program is just for a school project. Just have to Caesar Cipher it and add other layers like reversing it and adding irrelevant extra characters. The user can input a message and it encrypts it, then they can input the encrypted message back and it decrypts it. I'll be making that part now so if I run into any problems hopefully you can help me with that too lol.
    – Anth Math
    Aug 31, 2020 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.