| | |
New to java, Caesar Cipher question.
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
Well there are two parts of code I'm working on, one is a driver for Caesar Cipher and the other is the Caesar Cipher class itself. I can't figure out how to pass the information the user inputs from the driver class to the cipher class. Any help would be great, this is getting aggravating.
Java Syntax (Toggle Plain Text)
public class Driver { public static void main (String [] args) { Scanner in = new Scanner(System.in); // read input from terminal window CaesarCipher code; // an instance of a cipher String text; // user input to convert String result; // result of conversion int key; // key for Caesar cipher int menuSelection; // user's menu selection System.out.println("CS 160, Program 2: Caesar Cipher"); System.out.println("--------------------------------\n"); System.out.print("Enter the cipher key [0..25]: "); key = in.nextInt(); code = new CaesarCipher(key); displayMenu(); menuSelection = getMenuChoice(in); while (menuSelection != QUIT) { if (menuSelection == ENCODE) { System.out.print("Enter a sentence to encode: "); text = in.nextLine(); result = code.encode(text); System.out.println("Plain: " + text); System.out.println("Cipher: " + result); } else if (menuSelection == DECODE) { System.out.print("Enter numbers to decode: "); text = in.nextLine(); result = code.decode(text); System.out.println("Cipher: " + text); System.out.println("Plain: " + result); } else { System.out.println("Invalid option, please try again."); } displayMenu(); menuSelection = getMenuChoice(in); } System.out.println("\nGoodbye."); } private static void displayMenu() { System.out.println(); System.out.println("1. Encode a message."); System.out.println("2. Decode a message."); System.out.println("3. Quit."); } private static int getMenuChoice(Scanner in) { int menuChoice; // user's menu selection System.out.print("Please enter your menu choice: "); menuChoice = in.nextInt(); in.nextLine(); // clear the user input line return menuChoice; } public static final int ENCODE = 1; // user wants to encode text public static final int DECODE = 2; // user wants to decode numbers public static final int QUIT = 3; // user wants to quit }
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class CaesarCipher { public CaesarCipher(int key) { } public String encode(String text) { char current; //int code; this.text = text; for (int position = 0; position < text.length(); position++) { current = text.charAt(position); current = Character.toLowerCase(current); System.out.print(current + " "); int code = (int) current; //System.out.print(code + " "); if (Character.isLowerCase(current)) { code = code - A_VALUE; //+ key; System.out.print(code + " "); } //return code; } return "coded string"; } public String decode(String text) { return "decoded string"; } public int code; public String text; public int key; private static final int A_VALUE = (int) 'a'; }
![]() |
Other Threads in the Java Forum
- Previous Thread: heLLo!!
- Next Thread: Help, Help with Traffic Simulation program
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows





