Reputation: 193
The user can choose to shift the letters either left or right by choosing 1 for left or 2 for right. Left is working fine, right is not. Right now it shows exactly the same loop, but I have played with changing all the +
and -
signs in different ways and I always end up getting strange characters. How can I get the program to shift the characters in the opposite direction? If the user types Hi
, and the shift value is one and direction is right, the H
should become G
and the i
should become k
, as the amount to shift by should be increased by shiftValue
before each letter. Also, the first letter currently is unaltered, which should not be the case.
public static String rotate(String userString, int shiftValue, int shiftDirection) {
int myShift = 0;
shiftValue = shiftValue % 26 + 26;
StringBuilder encoded = new StringBuilder();
if (shiftDirection == 1) {
for (char i : userString.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' + myShift) % 26));
}
} else {
encoded.append(i);
}
myShift = (myShift + shiftValue) % 26;
}
} else if (shiftDirection == 2) {
for (char i : userString.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' + myShift) % 26));
}
} else {
encoded.append(i);
}
myShift = (myShift - shiftValue) % 26;
}
}
return encoded.toString();
}
Upvotes: 0
Views: 1155
Reputation: 1577
In your code, you set myShift
to 0 in the beginning and didn't change it when shifting your first letter. That is why your first letter is unaltered.
Do this before the actual rotation if you are shifting forward:
int myShift = shiftValue % 26 + 26;
I have answered your question already in your previous question, Rotating a shift each time a letter is read.
You can use an overloaded method for your rotate
method.
public static String rotate(String userString, int shiftValue, int shiftDirection) {
if (shiftDirection == 1) {
return rotate(userString, shiftValue);
}
else if (shiftDirection == 2) {
return rotate(userString, -shiftValue);
}
else {
return "This is not a valid way to shift your message.";
}
}
The other rotate
method would be the code that is repeated:
public static String rotate(String userString, int shiftValue) {
StringBuilder encoded = new StringBuilder();
int myShift = shiftValue % 26 + 26;
for (char i : userString.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + myShift) % 26 ));
} else {
encoded.append((char) ('a' + (i - 'a' + myShift) % 26 ));
}
} else {
encoded.append(i);
}
myShift = (myShift + shiftValue) % 26;
}
return encoded.toString();
}
Note that the two methods have the same name, but they take different parameters. When you call the method, use this one. Then you can specify which way you want to shift your message without worrying about changing the signs in your repeating code.
There are two other ways to make this work.
First, when shiftDirection == 2
, you should replace
encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
with
encoded.append((char) ('A' + (i - 'A' - myShift) % 26));
So the final for when shiftDirection == 2
should be
for (char i : userString.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' - myShift) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' - myShift) % 26));
}
} else {
encoded.append(i);
}
myShift = (myShift + shiftValue) % 26;
}
Second, you can have the direction shift backwards by setting myShift
to -shiftValue
in the beginning and keep everything else the same. The complete code for when shiftDirection == 2
should be
myShift = -shiftValue;
for (char i : userString.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' + myShift) % 26));
}
} else {
encoded.append(i);
}
myShift = (myShift - shiftValue) % 26;
}
Upvotes: 4