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 4 of 4

Thread: I cannot get the right output in the right place. Where is my problem?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I cannot get the right output in the right place. Where is my problem?

    this is the outpu that i am getting:

     Transpose
    The encrypted message is 
    wkh txlfn eurzq ira 
    The decrypted message is 
    the quick brown fox 
     
    The encrypted Transpose message is 
    wkh txlfn eurzq ira        
    The decripted Transpose message is 
    eht kciuq nworb xof       <--------------------- I need this to be on encrypted Transpose and here it should be the decrypted message like on the first one
     
    Reverser
    The encrypted Reverse message is 
     xof nworb kciuq eht 
    The decrypted Reverse message is 
    qeb nrfzh yoltk clu   <-------------------- I also need the decrypted message here like on the first one

    I believe the problem is in my methods (loop), but i have tried every possible configuration of the loop and this is as close as I can get it to the right output. Here is the code of all the classes.


    package assignment2;
     
    public class Caeser extends Cipher
    {
    	public Caeser(String s)
    	{
    		super(s);
    	}
    	public String encode(String word)
    	{
    		StringBuilder result = new StringBuilder();
     
    		for (int i = 0; i < word.length(); i ++)
    		{
    			char ch = word.charAt(i);
    			ch = determineCharacter(ch, Constants.ENCODE_SHIFT);
    			result.append(ch);
    		}
    		return result.toString();
    	}
     
    	public String decode(String word)
    	{
                StringBuilder result = new StringBuilder();
     
    		for (int i = 0; i < word.length(); i ++)
    		{
    			char ch = word.charAt(i);
    			ch = determineCharacter(ch, Constants.DECODE_SHIFT);
    			result.append(ch);
    		}
    		return result.toString();
     
     
    	}
     
    public char determineCharacter(char ch, int shift)
    	{
    		if(Character.isLowerCase(ch) || Character.isUpperCase(ch))
    			ch = (char)('a' + (ch - 'a' + shift) % Constants.WRAP_AROUND);
    		return ch;
    	}
    }
     
    package assignment2;
     
    public class Transpose extends Cipher
    {
    	Transpose(String s)
    	{
    		super(s);
    	}
    	public String encode(String word)
    	{
    	StringBuilder result = new StringBuilder();
     
     
    		for (int i= 0; i < word.length(); i++)
    		{
    			char ch = word.charAt(i);
    			ch = determineCharacter(ch, Constants.ENCODE_SHIFT);
    			result.append(ch);
    		}
    		return result.toString();
    	}
    	public String decode(String word)
    	{
    	StringBuilder result = new StringBuilder();
     
                    for (int i = word.length()-1;i >=0; i --)
                   {
                    char ch = word.charAt(i);
    			ch = determineCharacter(ch, Constants.DECODE_SHIFT);
    			result.append(ch);
    		}
    		return result.toString();
    	}
     
     
    public char determineCharacter(char ch, int shift)
    	{
    		if(Character.isLowerCase(ch) || Character.isUpperCase(ch))
    			ch = (char)('a' + (ch - 'a' + shift) % Constants.WRAP_AROUND);
    		return ch;
    	}
     
     
    }
    package assignment2;
     
     
    public class Reverser extends Transpose
    {
    	Reverser(String s)
    	{
    		super(s);
    	}
     
    	String reverseText(String word)
    	{
    	    StringBuilder result = new StringBuilder();
                for (int i = word.length()-1;i >=0; i --)
                    {
                    char ch = word.charAt(i);
    			ch = determineCharacter(ch, Constants.DECODE_SHIFT);
    			result.append(ch);
    		}
    		return result.toString();
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I cannot get the right output in the right place. Where is my problem?

    You need to try debugging the code by adding printlns to show the values of the variables as they change.

    Where is the driver code that calls the other code and prints out the results?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot get the right output in the right place. Where is my problem?

    Quote Originally Posted by Norm View Post
    You need to try debugging the code by adding printlns to show the values of the variables as they change.

    Where is the driver code that calls the other code and prints out the results?
    This?

    package assignment2;
     
    import javax.swing.JOptionPane;
     
    public class Main {
     
        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 = r.reverseText(r.getEncodedMessage());
            output += "\nReverser\nThe encrypted Reverse message is \n" + code + "\n";
            code = r.decode(code);
            output += "The decrypted Reverse message is \n" + code;
     
            System.out.println(output);
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I cannot get the right output in the right place. Where is my problem?

    Your code does not compile. Its missing the Cipher class and the Constants class

Similar Threads

  1. Problem with Output # of lines
    By coyboss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 10:21 PM
  2. problem with output
    By Timur in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 11th, 2011, 09:56 AM
  3. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 12:19 PM
  4. xml output problem
    By tsili in forum Java Theory & Questions
    Replies: 4
    Last Post: May 25th, 2010, 04:56 AM
  5. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 09:33 AM