I have to Implement a static public method named encodeCaesar
in the class Functionality.java
, which encodes a text using Caesar encryption and I am a complete novice in Java.
Signature:
encodeCaesar(String s, int val) : String
The method gets a string value and an integer value as input parameters. The letters (characters) from the string value are to be shifted by the integer value. For simplicity, I can assume that there are only letters and no spaces, numbers or special characters.
The string value should be converted to lower case before the encryption is performed. The method should return a string where each letter has been moved according to the specified integer value.
Example:
encodeCaesar("Ac",3)
returns"df"
. If the given integer value is less than 0 or greater than 26, an empty string should be returned.
public class Functionality {
public static void main(String[] args) {
}
public static String encodeCaesar(String s, int val) {
char[] newString = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
int newChar = newString[i] + val + 26;
// Handle uppercase letters
while (Character.isUpperCase(newString[i]) && newChar >= 65 + 26) {
newChar -= 26;
}
// Handle lowecase letters
while (Character.isLowerCase(newString[i]) && newChar >= 97 + 26) {
newChar -= 26;
}
newString[i] = (char) (newChar);
}
return String.valueOf(newString);
}
}
My problem is that in return it give me only true or false. How can I solve this: The method should return a string where each character has been moved according to the specified integer value.