Skip to content
Advertisement

JFrame freezes up when I press a button

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.

Advertisement

Answer

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.)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement