Hello- creating a typical caesar cypher however when I run, the program terminates after reading in only one integer... any thoughts on what could be causing this? Perhaps I need a method other than run?
Code
import java.util.Scanner;
public class Cipher {
public static void main(String[]args)
{
Cipher cipher = new Cipher();
cipher.run();
}
public void run(){
Scanner sc = new Scanner(System.in);
int shift = sc.nextInt();
String plaintext = sc.nextLine();
String ciphertext = encryptline(plaintext , shift);
System.out.println(ciphertext);
}
private String encryptline(String str, int shift){
String cipher = "";
for (int i=0; i< str.length(); i++){
char letter = encryptletter(str.charAt(i) , shift);
cipher += letter;
}
return cipher;
}
private char encryptletter(char letter, int shift){
if (Character.isLetter(letter)){
letter = (char) ('B' + (Character.toUpperCase(letter) - 'B' + shift) % 26);
}
return letter;
}
}
Changed sc.nextLine() to sc.next() and I can read one word, until I enter a space character. It reads nothing after that, however it performs the cipher correctly.
This post was edited by TheDiscoveryChannel on Jan 25 2013 03:01pm