Results 1 to 14 of 14
  1. #1
    Franneldort is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Smile Caesar and encoding with block spaces

    Thanks for looking at this post!

    In my java class we are doing some of our first encoding with the caesar and the caesar cipher, and our instructor wants to add something to it. The added part is the part I'm confused about. They are calling it a blockOut() and it is supposed to put a space every five letters when the program encodes the word the user enters as well and put a space every five letters when the word is decoded. Any help is greatly appreciated!

    Attached is my current program with part of the blockOut() in the CaesarVariation class. The Caesar class is my class that contains the actual encoding and decoding. And then there is the applet.
    Attached Files Attached Files

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    private StringBuffer blockOut(String s)
         {
    	 StringBuffer str=new StringBuffer();	
             s = decoded.getText();
        	 char[] sr=s.toCharArray();
    		
    	for(int i=0;i<sr.length;i++){
    		if(i%5==0){
    			str.append(" ");
    		}
    		str.append(sr[i]);
    		}
             
        	
        	 return str;
        	 
         }

  3. #3
    Franneldort is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Thank you, but how do get that method into the decode method. I tried calling it like a regular method, but it doesn't take...

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Show me how did you try to implement it?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Will your code add a blank in the first column? vs the fifth?
    Should this be before the test? instead of after
    str.append(sr[i]);

  6. #6
    Franneldort is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    I tried calling it like this:

    code:
    --------------
    public String decode(String secretWord, int shift, String s ) {
    StringBuffer result = new StringBuffer();
    String decodedWord = secretWord.toLowerCase();

    for (int k = 0; k < decodedWord.length(); k++) {
    char ch = decodedWord.charAt(k);
    // System.out.print(ch + " ");
    blocks.blockOut(s);
    if ((Character.isLetter(ch))) {
    ch = (char) ('a' + (ch - 'a' + 26 - shift) % 26);
    result.append(ch);
    shift = (shift + 1) % 26;

    // System.out.println(" this is char " + ch + " this is k " +k+
    // " this is shift "+ shift+ "");
    }

    }// decode
    return result.toString();
    }
    -------------
    and I used blocks as this:

    private CaesarVariation blocks;

    and then called it like that.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    blockOut returns a StringBuffer that you need to receive in a variable.

  8. #8
    Franneldort is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    You mean assign it a value like this?

    String str = blocks.blockOut(s);

    I'm very new to java. Perhaps you could explain what you mean by receive in a variable? :)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Yes, that's almost it. Since it returns a StringBuffer it should be:
    StringBuffer str = blocks.blockOut(s);

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default yeap...

    Yes ... the blockOut method retuns a StringBuffer, not a String. Therefore it would be something like:
    Java Code:
    StringBuffer strBuff = blocks.blockOut(s);
    And while I'm at it here are some links to see what StringBuffer methods are availble and what they can do:

    The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    StringBuffer (Java Platform SE 6)
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    if(i%5==0){
    should be
    if(i%5==4){
    To add blank after 5 characters have been copied

  12. #12
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Quote Originally Posted by Norm View Post
    if(i%5==0){
    should be
    if(i%5==4){
    To add blank after 5 characters have been copied
    Ah yes,then there won't be a space at the beginning of the StringBuffer ;) ,thanks Norm

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Well you gave the OP the code, it is up to him to get it to work the way he wants.
    One other change I would make is:
    private StringBuffer blockOut(String s, int insertAfter)
    ...
    if(i%insertAfter==(insertAfter-1)){

    With tests for insertAfter == 0.

  14. #14
    Franneldort is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

Similar Threads

  1. How to specify character encoding in JavaMail?
    By jfcup in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-24-2008, 07:55 PM
  2. Some help with encoding...
    By nm123 in forum Networking
    Replies: 0
    Last Post: 04-15-2008, 12:22 AM
  3. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 09:40 PM
  4. Help with a word, if it is divided by spaces
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:31 AM
  5. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •