Simple Java Shift Encryption

Hello everyone!

I am new to the world of java programming, and I am working on basic shift encrytion. Here is what I need the program to do:

User inputs a single lower-case letter or upper-case letter or number (0-9).

User inputs number of places to shift letter or number (i.e. user types in 0 and shifts 2, the encrypted output is 2).

I have the code working to shift the input successfully, but I can't get the numbers or letters to wrap around. I need to limit this to letters and numbers only, no special characters.

I am converting the input char to its ASCII value to do the operations.

Any suggestions on how to detect input and do the wrap around?

Example of what I mean by wrap around:

User inputs Z and shifts 2 places, should be come a B, not a \.

Thank you in advance!

Kody
LVL 6
Kody-BurgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kody-BurgAuthor Commented:
I believe what I am trying to do is make a very simple single char input caesar shift, limited to only letters and numbers.
0
for_yanCommented:
Sorry, I wanted some clarification - are you talking about
real ASCI representation or
you  say have sequence

"ABCDEFGHIJKLMNOPQRASTUVWXYZ"

and you want to shift within this sequence and wrap around  if necessary

Becuase if you do something with ASCII representations you'll of course need to go
out of this set,

So then do you shift numbers within numbers and leters within letters?

If this is so, I think you can implement wrap around using "%" operator (modulo n)

If this is about real ASCII representation then something else is required







 
0
for_yanCommented:

So let's suppose you have them all in one buig sequence:

String all = "ABCDEFGHIJKLMNOPQRASTUVWXYZabcdefghijklmnopqrstuvw0123456789"

Then let's think you input is in String s

String s = "M";

int i = all.indexOf(s);

then your new charcter

will be


String s1 = "" + all.charAt((i+2) % 62)

where 62 is the total number of charcaters in you big string

Then eve if s = "9" you'll get in this way to charcater "B" - hope that is waht you want


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Kody-BurgAuthor Commented:
What I need to do is this:

If input is a number, stay within that range. EX: input is 0 so you can only shift in the range 0 - 9

if input is a capital letter, stay within that range EX: input is A so you can only shift in the range of A-Z

Does this make sense? So if I'm at a capital Z, and enter shift 2, it should become a capital B.

0
Kody-BurgAuthor Commented:
I was able to make it work by modifying the code that you provided for me. Thank you!
0
for_yanCommented:
Well then the same idea as I showed above.

This is the static method which can do it



static  String [] MyStrings = {
"0123456789",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyz",

}

input character is in String s:

public static string Encode(String s, int shift){


String snew = null;

for (int j=0; j<myStrings.length; j++){
if(myStrings[j].indexOf(s) == -1)continue;

snew = "" + myStrings[j].charAt((myStrings[j].indexOf(s) + shift)%myStrings[j].length());


}

if(snew == null){
System.out.println("It was not digit opr letter");
}
else
System.out.println(snew);


}
return snew;

}


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.