Help with Java Encryption Method

gflores

Senior member
Jul 10, 2003
999
0
0
I need some help writing a java program that takes in 2 characters and then encrypts the first char by moving the character x number of time to the right, x being the second character. It then returns the encrypted character.
For example:

public char encrypt(char s, char b)
{
// moves character 's' 2 spaces (because 'b' is the 2nd letter in the alphabet).

return encryptChar;
}

Any ideas on how to do this? Thanks.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
something like:

int intShift = (int)Character.toUpperCase(b) - (int)'A';
return (char)((int)s + intShift);
 

gflores

Senior member
Jul 10, 2003
999
0
0
Ok, that looks right. However, what happens when the two chars are say... 'x' and 'd'. Then 'x' has to wrap around so that it'll be 'b', how would I go doing that? Thanks.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Have you learned about "if" statements yet?

as in:
if (the letter needs to wrap around)
do wrappy-shift
else
do the normal-shift