-5

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);
nickb
  • 59,313
  • 13
  • 108
  • 143
Jay
  • 3
  • 6
  • 1
    `char secretLetter = 'a' + keyLength - 1;` – that other guy Jan 11 '17 at 23:49
  • Maybe also consider having a look at [this previous question](http://stackoverflow.com/questions/41580795/cant-shift-caesar-cipher-by-more-than-1) – MadProgrammer Jan 11 '17 at 23:49
  • What you are doing is wrong. Hint: `String` is a `char`acter array. So in the for loop try to change each character with the secretletter. – Pritam Banerjee Jan 11 '17 at 23:50
  • Relate the ASCII value to the key. – ChiefTwoPencils Jan 11 '17 at 23:50
  • Hint: you can treat characters as integers representing their indexes in Unicode Table. So if you do `'a'+1` you will get `int` representing index of next character after `'a'` which is `'b'`. You can also cast `int` to `char`. No loops are needed. – Pshemo Jan 11 '17 at 23:50
  • @that other guy Thank you so much! I just realized and then you posted it as well! – Jay Jan 11 '17 at 23:50
  • Since your question is not really about loops use [edit] option to clarify it (especially title). – Pshemo Jan 11 '17 at 23:51
  • 1
    Your loop consecutively assigns a new value to `secretletter` (bad name because it violates the Java conventions), removing each preceding assignment's result in the process. Only the last assignment to `'z'` remains at the end. Also, the initialization to `0` is wasted, as that value is also immediately forgotten before it's ever used. At no point does the loop use the `keyLength` value to choose what gets assigned. – Lew Bloch Jan 11 '17 at 23:56

1 Answers1

0

You may want to take a look at Oracle documentation for the switch statement.

public static char secret(int keyLength){
  char key;
  switch (keyLength) {
    case 1:  key = 'a';
      break;
    case 2:  key = 'b';
      break;
    case 3:  key = 'c';
      break;
    ........
    default: key = 'z';
      break;
  }
  return key;
}
James Jayson
  • 336
  • 1
  • 7
  • (1) code-snippet is for code which can be actually run by browser, so this tool is for CSS/JavaScript/HTML examples. For Java use code-sample (`{}` icon in editors menu). (2) `char key = '';` will not compile since `''` is not valid character. – Pshemo Jan 11 '17 at 23:57