-1

I'm working on Caesar cipher example in which I want that it get different keys from user and finally one key decrypt the original text , but I got a problem, here is my code

public static void main(String[] args) {
    Scanner user_input= new Scanner(System.in);
    String plainText = "University of malakand";
    String key;
    key = user_input.next();

    Ceasercipher cc = new Ceasercipher();

    String cipherText = cc.encrypt(plainText,key);
    System.out.println("Your Plain  Text :" + plainText);
    System.out.println("Your Cipher Text :" + cipherText);

    String cPlainText = cc.decrypt(cipherText,key);
    System.out.println("Your Plain Text  :" + cPlainText);
}

it shows an error on this line

    String cipherText = cc.encrypt(plainText,key);

it shows me error on key incompatible types:

String cannot be converted into int

What can I do?

ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
HanifCs
  • 119
  • 3
  • 9

4 Answers4

2

Ask following questions to your self first.

  • What your method want as parameter?
  • Why both are Incompatible?
  • What String cipherText = cc.encrypt(plainText,key); mean?
  • key is String or int?

Use methods like Integer.parseInt(String value) or Integer.valueOf(String value) for conversion.

akash
  • 22,224
  • 10
  • 57
  • 85
1

It seems like you need to convert a String to int, not a int to String. To do that, you can use Integer.parseInt():

int someInt = Integer.parseInt(someString);
Christian Tapia
  • 32,670
  • 6
  • 50
  • 72
0

Try this -

String cipherText = cc.encrypt(plainText,Integer.parseInt(key));
Erran Morad
  • 4,333
  • 10
  • 39
  • 69
Amit
  • 290
  • 1
  • 10
0

You are passing String and the method parameter seems to have int and hence the error. You might want to convert your string to int using int keyInt = Integer.parseInt(key); and similarly for plain text if necessary and then pass keyInt and/or plainTextInt as the parameters.

anirudh
  • 3,920
  • 2
  • 18
  • 35