I am new to Java and this a very basic question. This is a very small part of the program. Here is what I have to do:
User inputs a number (1-26) in keyLength
as an encryption. If number is 1, secretletter
would be a; if its 2 it would be b, and so on until z where it will be 26.
What I want to know is that, is there a better way than doing this:
if (keyLength==1){
secretletter=a;
if (keyLength==2){
secretletter=b;
What I have done is below, but my for loop is wrong and I always end up with z.
public static char secret(String str,int keyLength){
char secretletter = 0;
if (keyLength>=0 && keyLength<27){
for(char i='a'; i<='z';i++){
secretletter=i;
}
}
return secretletter;
}
EDIT: I found my mistake and @that other guy helped me as well
secretletter=(char)('a'+ keyLength - 1);