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