Afternoon everyone
I have an issue with an assignment which is very similar to those of this thread:
http://www.javaprogrammingforums.com...y-problem.html
The assignment requires us to:
1. Take user input
2. Display original message
3. Encode the message using Caesar shift
4. Decode the above
5. Encode message using Transpose
6. Decode above
7. Reverse message using Reverser
8. Decode above
All of this on one option pane. Most of the code was given to us by the professor. However once i connected everything, it does not display anything past the original message. It stays running and doesnt do anything. Not even in command console.
Below are my two classes.
Cipher class
package cipher; import java.util.Scanner; import java.util.StringTokenizer; import javax.swing.JOptionPane; public abstract class Cipher { public String message; StringBuilder encrypted_message, decrypted_message; public Cipher(String text) { Scanner in = new Scanner(System.in); text = in.next(); message = text; } public final void encrypt() { /* The message string is tokenized into individual words, * andco each word is encoded by calling the encode method */ encrypted_message = new StringBuilder(); StringTokenizer words = new StringTokenizer(message); while(words.hasMoreTokens()) { String s = words.nextToken(); s = encode(s) + " "; encrypted_message.append(s); } } public final void decrypt(String message) { /* The encoded message string is tokenized into individual words, * and each word is encoded by calling the decode method */ // Supply the code that will decrypt the encrypted string } public String getEncodedMessage() { return encrypted_message.toString(); } public String getDecodedMessage() { return decrypted_message.toString(); } public abstract String encode(String s); public abstract String decode(String s); public static void main(String arg[]) { String code, output = ""; String text = JOptionPane.showInputDialog("Enter message"); output += "The original message is \n" + text + "\n"; Cipher c = new Caeser(text); c.encrypt(); code = c.getEncodedMessage(); output += "\nCeasar Cipher\nThe encrypted message is \n" + code + "\n"; c.decrypt(code); code = c.getDecodedMessage(); output += "The decrypted message is \n" + code + "\n"; c = new Transpose(text); c.encrypt(); code = c.getEncodedMessage(); output += "\nTranspose\nThe encrypted Transpose message is \n" + code + "\n"; c.decrypt(code); code = c.getDecodedMessage(); output +="The decripted Transpose message is \n" + code + "\n"; Reverser r = new Reverser(text); r.encrypt(); code = c.getEncodedMessage(); code = r.reverseText(code); output += "\nReverser\nThe encrypted Reverse message is \n" + code+ "\n"; code = r.decode(code); output+="The decrypted Reverse message is \n" + code; display(output); } static void display(String s) { JOptionPane.showMessageDialog(null, s, "Encrypt/decrypt", JOptionPane.INFORMATION_MESSAGE); } }
Caeser class:
package cipher; public class Caeser extends Cipher { public Caeser(String s) { super(s); } @Override public String encode(String word) { return code(word,Constants.ENCODE_SHIFT ); } @Override public String decode(String word) { return code(word,Constants.DECODE_SHIFT ); } String code(String word, int SHIFT) { StringBuilder result; result = new StringBuilder(); for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); ch = determineCharacter(ch, SHIFT); result.append(ch); } return result.toString(); } public char determineCharacter(char ch, final int shift) { if(Character.isLowerCase(ch)) ch = (char)('a' + (ch - 'a' + shift) % Constants.WRAP_AROUND); else ch = (char)('A' + (ch - 'A' + shift) % Constants.WRAP_AROUND); // Complete the if/else so that lower case letters are accounted for return ch; } }
Unlike the person in the other thread, i get no output. I dont see what im doing wrong. I know some of it is incomplete, but i want to get past the encryption part to worry then later about everything else.
Thanks for your help