d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Method/scanner Question
Add Reply New Topic New Poll
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Jan 25 2013 02:48pm
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
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Jan 25 2013 05:27pm
Quote (TheDiscoveryChannel @ Jan 25 2013 12:48pm)
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.


added sc.nextLine() back and and an unassigned sc.nextLine to eat the newline. Program works!
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Jan 29 2013 01:37pm
FYI- I found out a little trick that seems handy for avoiding this simple program issue:

If you're doing a lot of input, you can define a quick nextIntLine() method which does basically this - reads a nextInt, then consumes a newline and returns the int. You can even set it to loop until an int is entered, rather than throwing a NumberFormatException (or actually an InputMismatch, I think it is)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll