-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
  • 54,432
  • 29
  • 203
  • 199
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,664
  • 11
  • 59
  • 87
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
  • 33,620
  • 7
  • 56
  • 73
0

Try this -

String cipherText = cc.encrypt(plainText,Integer.parseInt(key));
Erran Morad
  • 4,563
  • 10
  • 43
  • 72
Amit
  • 290
  • 2
  • 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
  • 4,116
  • 2
  • 20
  • 35