Tengo problemas para ejecutar mi código correctamente. Creé un método de descifrado que se supone que toma una palabra y reemplaza cada 2 letras entre sí. A menos que la palabra sea una cantidad impar, entonces debo dejar la última letra en paz. El problema es que solo imprimo las letras y no cambio los datos reales de la cadena.
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; }Entiendo que está tratando de modificar una cadena para descifrarla. Bueno, tengo algunas noticias para ti: la clase String en Java ha sido diseñada de tal manera que los objetos String son inmutables. Eso significa que no puede modificar sus contenidos una vez que los haya creado. Pero no te preocupes, hay otras formas de lograr lo que tienes en mente.
Por ejemplo, puede obtener una matriz de caracteres del objeto recibido llamando a ezEncrypt.toCharArray() ; puede modificar el contenido de una matriz, por lo que tendrá que trabajar con eso, intercambiando los caracteres como se supone que debe hacerlo. Luego, una vez que se realiza el descifrado, cree otro objeto String utilizando el constructor new String(char[] chars) , pase su matriz como argumento y devuélvalo.
Algo más o menos así:
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); }Espero que esto te ayude.
Las cadenas son inmutables, por lo que llamar a un método en la cadena no cambiará la cadena. Solo devolverá un valor derivado de la cadena. Debe crear una nueva cadena vacía y comenzar a agregarle los valores de retorno carácter por carácter.