2014-04-20 5 views
-1

나는 사용자로부터 다른 키를 얻고 원 키를 해독하기를 원하는 시저 코드 예제를 작성하고 있습니다.하지만 문제가 있습니다. 여기에 내 코드가 있습니다.문자열을 Java에서 int로 변환하는 방법

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); 
} 

은 그것이 키 호환되지 않는 유형에 나에게 오류를 보여줍니다이 줄

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

에 오류를 보여줍니다

문자열 int로 변환 할 수 없습니다.

어떻게해야합니까?

+3

먼저 '문자열을 int로 변환 할 수 없습니다'는 것을 의미합니까? –

+1

입력 한 값을 표시 할 필요가 없다고 생각하십니까? –

+0

고마워요. – HanifCs

답변

1

Stringint으로 바꾸고 intString으로 변환하지 않아도되는 것 같습니다. 당신은 문자열을 전달하는

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

이 시도 : 그렇게하려면, 당신은 Integer.parseInt()를 사용할 수 있습니다. stringint으로 변환하려면 keyInt = Integer.parseInt(key);을 사용하고 필요에 따라 일반 텍스트를 입력 한 다음 keyInt 및/또는 plainTextInt를 매개 변수로 전달할 수 있습니다.

0

및 방법 매개 변수는 INT 따라서 오류가있는 것 -

int someInt = Integer.parseInt(someString); 
2

먼저 다음 질문을하십시오.

  • 매개 변수로 원하는 것은 무엇입니까?
  • 둘 다 왜 호환되지 않습니까?
  • 무엇이 String cipherText = cc.encrypt(plainText,key);입니까?
  • 키가 String 또는 int입니까?

변환을 위해 Integer.parseInt(String value) 또는 Integer.valueOf(String value)과 같은 방법을 사용하십시오.

관련 문제