0

I'm just a starter at java, and I'm trying to figure out the codingbat "Ceaser Cipher" problem. The questions says to "Develop a method that accepts as input a shift value and a message. The shift value will be no less than -25 and no greater than 25. Any character that occurs in the message and that is not an upper case letter should be encoded as itself." The program already applies the message and value. For example, if the message is "ABCDE" and the shift is 1, it should print "BCDEF".

Eveyrtime I try to run my code, I get a "char cannot be converted to java.lang.String line:9" error. Does anyone know how to fix this?

public String CaesarCipher(int shift, String message) {
  for (int i = 0;i < message.length(); i++){
     char letter = message.charAt(i);

     if (Character.isUpperCase(letter)){
      int ascii = (int)letter;
      ascii += shift;
      char newMsg = (char)ascii;
      return newMsg;
     }

     else{
      return letter;
     }
  }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
rether
  • 5
  • 3

4 Answers4

0

You are trying to return a Character when the method requires you to return a String. Instead of returning newMsg or letter, you want to create a new string at the beginning of the method and set the next character the string to that value, like this:

public String CaeserCipher(int shift, String message) {
  String updated_message = "";
  for (int i = 0; i < message.length(); i++) {
    ...
    char newMsg = (char)ascii;
    updated_message += newMsg;
    ...
    updated_message += letter;
  } // end for
  return updated_message;
}
Austin Adams
  • 231
  • 1
  • 10
  • 1
    `message.charAt(i)` is *not* an l-value. `String`s are immutable. – Matthieu Nov 02 '19 at 22:28
  • 1
    Matthieu is correct. I updated my answer with the correct way to go about this (create an empty string at the beginning and add to it in each iteration of the loop) – Austin Adams Nov 02 '19 at 22:44
0

That error code implies that return statement is not giving right type of variable. Your method wants String back but is getting char. This is easily solved by changing your char to String with String.valueOf(newMsg).

public static String CaesarCipher(int shift, String message) {
    for (int i = 0;i < message.length(); i++){
     char letter = message.charAt(i);

     if (Character.isUpperCase(letter)){
      int ascii = (int)letter;
      ascii += shift;
      char newMsg = (char)ascii;
      return String.valueOf(newMsg);
     }

     else{
      return String.valueOf(letter);
     }
    }
    //should not come to this
    return "a";
    }
Samuel
  • 52
  • 7
0

Your code doesn't work as you expect. First iteration of the for loop will always exit because return statement. There are also redundant variables and casts. Even a free Java IDE like IntelliJ IDEA Community will highlight these problems so please consider installing one.

What you want to do is to build the result with a StringBuilder and return it after the loop completes:

public static String caesarCipher(int shift, String message) {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < message.length(); i++) {
        char letter = message.charAt(i);
        if (Character.isUpperCase(letter)) {
            char shiftLetter = (char) (letter + shift);
            builder.append(shiftLetter);
        }
    }
    return builder.toString();
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
0

That is because you're trying to return a char while the return type of your CaesarCipher function is String. In Java char and String are different data types, meaning that they are not interchangeable, the same goes for all the data types. You'd get a warning if you were using an IDE, so I'd suggest you write your code in an IDE before pasting it to Coding Bat.

This might help: How to convert ASCII to String

I believe this is what you need:

public String CaesarCipher(int shift, String message) {
  String result = "";
  for (int i = 0; i < message.length(); i++) {
    char letter = message.charAt(i);

    if (Character.isUpperCase(letter)) {
      int ascii = (int) letter;
      ascii += shift;
      result += Character.toString((char)ascii);
    } else {
      result += Character.toString(letter);
    }
  }
  return result;
}
Kenan Soylu
  • 116
  • 2
  • 7