Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Issue with Caeser Shift, Transpose, and Reverser

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issue with Caeser Shift, Transpose, and Reverser

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Issue with Caeser Shift, Transpose, and Reverser

    Modifying the variable 'output' doesn't actually cause a message to be output to the user via a JOptionPane. You have to write the code that does that. Follow the initial output as an example.

Similar Threads

  1. java shift and ~ operator when to use each one?
    By tonu in forum Java Theory & Questions
    Replies: 1
    Last Post: December 27th, 2013, 12:08 PM
  2. Replies: 2
    Last Post: July 15th, 2012, 09:19 AM
  3. deleting with shift ?
    By keep smiling in forum Collections and Generics
    Replies: 4
    Last Post: February 26th, 2012, 04:05 PM
  4. [SOLVED] Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 19th, 2011, 04:18 PM
  5. How to shift a String in a rectangle which is type of Canvas
    By elenora in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: April 4th, 2011, 07:39 AM