Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Decryption code not changing actual data

I am having issues running my code correctly. I created a decryption method that is supposed to take a word and replace every 2 letters with each other. Unless the word is an odd amount, then I should leave the last letter alone. The issue is that I am only printing out the letters and not changing the actual data of the string.

static String ezDecrypt (String ezEncrypt){

    //this variable holds the value of the String's length
    int cl = ezEncrypt() ;

    //if the length of the String is even then do this
    if (ezEncrypt.length() % 2 == 0){

        //for loop that begins at 0
        //keeps looping until it reaches the end of the string
        //each loop adds 2 to the loop
        for(int i = 0; i < cl; i= i + 2) {
            //will print out the second letter in the string
            System.out.print(ezEncrypt.charAt(i + 1));
            //will print out the first letter in the string 
            System.out.print(ezEncrypt.charAt(i));             
        }
    }
    //if the length of the word is an odd number, then 
    else if(ezEncrypt.length() % 2 != 0){
        //loop through and do the same process as above
        //except leave this loop will skip the last letter
        for(int i = 0; i < cl-1; i= i + 2) {
            //will print out the second letter in the string
            System.out.print(ezEncrypt.charAt(i + 1));
            //will print out the first letter in the string 
            System.out.print(ezEncrypt.charAt(i));  
        }
    }
    return ezEncrypt;

}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I understand you're trying to modify a String in order to decrypt it. Well, I got some news for you: the String class in java has been designed in such a way String objects are immutable. That means you can't alter their contents once you have created them. But don't worry, there are other ways to achieve what you have in mind.

For instance, you can get an array of chars from the received object by calling ezEncrypt.toCharArray(); you can modify the contents of an array so you will have to work with that, swapping the characters just like you're supposed to. Then, once the decryption is done, create another String object by using the constructor new String(char[] chars), passing your array as argument, and return it.

Something more or less like this:

static String ezDecrypt (String ezEncrypt){
    //this variable holds the value of the String's length
    int cl = ezEncrypt.length();

    //an array holding each character of the originally received text
    char[] chars = ezEncrypt.toCharArray();

    //temporary space for a lonely character
    char tempChar;

    //Do your swapping here
    if (ezEncrypt.length() % 2 == 0){ //Length is even
        //for loop that begins at 0
        //keeps looping until it reaches the end of the string
        //each loop adds 2 to the loop
        for(int i = 0; i < cl; i = i + 2) {
            tempChar = chars[i];
            chars[i] = chars[i+1];
            chars[i+1] = tempChar;
        }
    } else { //Length is odd
        //loop through and do the same process as above
        //except leave this loop will skip the last letter
        for(int i = 0; i < cl - 1; i = i + 2) {
            tempChar = chars[i];
            chars[i] = chars[i+1];
            chars[i+1] = tempChar;
        }
    }
    return new String(chars);
}

Hope this helps you.

over 4 years ago · Santiago Trujillo Report

0

Strings are immutable, so calling a method on the string will not change the string. It will only return a value derived from the string. You need to make a new empty string and start adding the return values to it character by character.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!