Results 1 to 14 of 14
- 10-29-2008, 10:26 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
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.
- 10-29-2008, 10:49 PM #2
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; }
- 10-29-2008, 11:49 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
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...
- 10-29-2008, 11:52 PM #4
Show me how did you try to implement it?
- 10-29-2008, 11:55 PM #5
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]);
- 10-30-2008, 12:08 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
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.
- 10-30-2008, 12:17 AM #7
blockOut returns a StringBuffer that you need to receive in a variable.
- 10-30-2008, 12:20 AM #8
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
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? :)
- 10-30-2008, 02:26 AM #9
Yes, that's almost it. Since it returns a StringBuffer it should be:
StringBuffer str = blocks.blockOut(s);
- 10-30-2008, 02:32 AM #10
yeap...
Yes ... the blockOut method retuns a StringBuffer, not a String. Therefore it would be something like:
And while I'm at it here are some links to see what StringBuffer methods are availble and what they can do:Java Code:StringBuffer strBuff = blocks.blockOut(s);
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.
- 10-30-2008, 02:15 PM #11
if(i%5==0){
should be
if(i%5==4){
To add blank after 5 characters have been copied
- 10-30-2008, 02:21 PM #12
- 10-30-2008, 03:28 PM #13
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.
- 10-30-2008, 04:48 PM #14
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
How to specify character encoding in JavaMail?
By jfcup in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 10-24-2008, 07:55 PM -
Some help with encoding...
By nm123 in forum NetworkingReplies: 0Last Post: 04-15-2008, 12:22 AM -
Load URL that contains spaces?
By barkster in forum Java AppletsReplies: 0Last Post: 01-30-2008, 09:40 PM -
Help with a word, if it is divided by spaces
By baltimore in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:31 AM -
Reading file data that contains no spaces
By jdepue in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks