I have to write a program (method thingy, excuse my noobness)that has a char array as an input, converts it to numbers according to its ascii value, shifts it to whatever the inputed shift value (can be positive or negative) is, and then decrypts it.
the problem with my program is that it works for nearly the whole array, but for certain test cases fails for the last few characters. Can anyone help point out flaws in my logic?
For example, sgADF@#$5^^%{].
reads sgADF@#$5^^%{g3
after decryption (i.e. ].
is replaced with g3
).
I'm pretty sure there's no problem with my decrypt. it just inputs the line array (should be char) into the encrypt with the opposite shift value.
//converts char values to numbers and stores them in hold array
for (int x = 0; x < line.length; x++) {
char character = line[x];
int ascii = (int) character;
hold[x] = ascii;
}
[![enter image description here][1]][1]//shift to numbers
for (int x = 0; x < line.length; x++) {
//case1
if (nshift < 0) {
while (hold[x] + nshift < THIRTY_TWO) {
nshift = nshift + NINETY_FIVE;
// nshift = copy of shift (checkstyle)
}
hold[x] = hold[x] + nshift;
}
//case2,
if (shift > 0) {
while (hold[x] + nshift > ONE_TWENTY_SIX) {
nshift = nshift - NINETY_FIVE;
// nshift = copy of shift (checkstyle)
}
hold[x] = hold[x] + nshift;
}
}
for (int x = 0; x < hold.length; x++) {
char digit = (char) hold[x];
out[x] = digit;
}
return out;