When I run this in eclipse, it all appears to go well, but after I type either 0 or 1 to select a cipher and hit enter, it just prints the "now enter your ciphertext" stuff and then terminates itself without letting you type anything in.
Any idea why it would terminate before it gets to the second scan? Does it have something to do with eclipse? This is the first project I've ever made in it (Finally switched IDEs) so I don't really know how it's terminal or coding standards or whatever work.
Code:
Code:package cracker; import java.util.Scanner; class crack { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int cipher; String ciphertext; int shift; int numArray[] = new int[26]; System.out.println("Cipher Cracker - By 323"); System.out.println("----Written in Java----"); System.out.println("Version 1.0 -- 5/3/2013"); System.out.println("What type of Cipher would you like to crack?"); System.out.println("0 - Caesar Shift Cipher"); System.out.println("1 - Atbash Cipher"); cipher = scan.nextInt(); //-----Caesar Shift Cipher Cracker-----// if (cipher == 0) { System.out.println("Okay, we'll be cracking a Caesar Shift cipher."); System.out.println("First, enter your ciphertext (The encrypted text):"); ciphertext = scan.nextLine(); //user enters ciphertext char[] charArray = ciphertext.toCharArray(); //string to char array of ciphertext for (int i=0; i < ciphertext.length(); i++) { numArray[i] = charArray[i] - 'a' + 1; //put ciphertext into num form } System.out.println("How much would you like to shift?"); shift = scan.nextInt(); //set shift to user inputted value } //-----Atbash Cipher Cracker-----// else if (cipher == 1) { System.out.println("Okay, we'll be cracking an Atbash cipher."); System.out.println("Please enter your ciphertext (The encrypted text):"); ciphertext = scan.nextLine(); //user enters ciphertext char[] charArray = ciphertext.toCharArray(); //string to char array of ciphertext for (int i=0; i < ciphertext.length(); i++) { numArray[i] = charArray[i] - 'a' + 1; //put ciphertext into num form } for (int i=0; i < ciphertext.length(); i++) { System.out.println(charArray[i]); System.out.println(numArray[i]); } } //-----Error Handling-----// else { //in case user enters errornous input System.out.println("How did you even get here? ERRCODE: ELSE-C"); } scan.close(); } }
Results 1 to 3 of 3
Thread: Shit, I'm so confused
- 06 May. 2013 02:20am #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 12.22
Shit, I'm so confused
- 06 May. 2013 12:50pm #2
Basic Java scanner class rules say that nextInt simply reads the next integer, not the line break that comes with your input.
When you continue on to the next nextLine input the scanner reads that line break and immediatly terminates the user input.
Therefore you need something to absorb that line break before calling nextLine.
I'd recommend simply adding scan.nextLine() after this section:
Code:System.out.println("1 - Atbash Cipher"); cipher = scan.nextInt();
Last edited by Isonyx; 06 May. 2013 at 01:20pm.
I don't get tired.
- 08 May. 2013 06:16pm #3
I'd suggest using ciphertext = scan.next(); instead of nextLine()
Also, the code will error when ciphertext.length() >= 26 (hint numArray)