Need hep with JAVA...Any IT experts...

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

Hay guys ...I am trying to shift any word or character by fixed shifter but I really dont know how to append the each shifted character i mean encrypted character...Anyhelp will be appreciated.  My doCipher() just returns the last encoded charcter not all the other letters. I assume it do encrypt all but only shows the last.  JUst need to work on doCipher() for now...Thank you guys...   /********  * Cipher class -  * A class that is used to implement the Caesar cipher.  *  * @author song  *  */ public class Cipher {     String plainText;     /*******      * Constructor for a Cipher object      * @param plainText the document text as a String.  We copy      * the param (for protection), and lower case it.      */     public Cipher(String plainText) {         this.plainText = plainText.toLowerCase();     }     /**************      * Returns the encrypted plainText, with 'a' being the value of shift      * @param shift The character to use in place of 'a'.  Must be in the range      *              'a' .. 'z', or we throw an exception.      * @return the encrypted plainText as a String      */     public String doCipher(char shift) {         String cipherText= null;         if (shift < 'a' || shift > 'z') {             throw new IllegalArgumentException("shift amount must be between lowercase a and z");         }                         int newShift = shift -'a';         for (int i=0; i < plainText.length(); i++) {         char decode = (char)(plainText.charAt(i) + newShift);         char[] done = {decode};         cipherText= new String(done);         }                return cipherText;     }     /***************      * Returns the encrypted plainText, with 'a' being shift + 1 (right-shifted)      * @param shift The character before the one to use in place of 'a'      * @return the encrypted plainText as a String      */     public String nextCipher(char shift) {         // TODO: Complete me!         return null;     }     /**************      * Returns the encrypted plainText, with 'a' being shift - 1 (left-shifted)      * @param shift The character after the one to use in place of 'a'      * @return the encrypted plainText as a String      */     public String prevCipher(char shift) {         // TODO: Complete me!         return null;     } }**

refugeeNP · Feb 10, 2010 1:00 AM · 11,837 views

2 Replies

professional  ta hoina,  tara eikdum  sikaru  ni hoina,    yeso  code padhdha  ra  problem  chain  eua character  matrai   return  bhayecha  bhanda  kheri  problem  yo  chain  line  ma  bahye   jasto   lagyo: for (int i=0; i < plainText.length(); i++) {         char decode = (char)(plainText.charAt(i) + newShift);            char[] done = {decode};         cipherText= new String(done);         } yahan  new String  chain  pani  loop   ko  bhitra  parecha,  so it  creates a  new  string  object every  time u  go  through the  loop,  storing  only  one   character that is  stored in  done.   therefore,   it returns   the last  character. also,   String in java is  "fixed" in a sense  that u cannot append  i t,   so  once u  create a  string,  changing  somethign in  it  basically  means  u have to create a new string... which is  what  u seem to  be  doing,  but if  that is what  u are  trying  to do,   u   might  need  one   to do  something  on this  line: cipherText = new String(null); // i am not sure if u need this,  since i  am not sure if String by default         // gets set to null. for (int i=0; i < plainText.length(); i++) {         char decode = (char)(plainText.charAt(i) + newShift);            char[] done = {decode};         cipherText= new String(cipherText + done);         } a + with a string  on either side will try  to perform  the concat,   i am not sure  hoewever  if the compiler can automatically cast the char array as  string. if not,  u  may not  need the aray at all, and  just  do: cipherText= new String(cipherText + decode); this  from what i remember  might  work. this however, is  not the most efficient way, i think, since  u are creating a new string every time in the loop. a better  way  would  be to  use  StringBuilder / StringBuffer. here is a java doc link to stringbuilder http://java.sun.com/javase/6/d... so f u declare cipherText as  Stringbuilder ,  then u can  use  something on the lines of  cipherText.append(decode) to  build the  string, then  return  it as : cipherText.toString(); since  u r  trying to return a String type  object. don't  know  ur level of expertise,  but since  the  q is about  encryption,  i am  assuming u are familiar with  java  docs. timilai java  doc  padhne  baani chaina/ aunna  bhane  let me know,   ali ali explain  garne   koshish  garumla. solution  thyakkai chahin  k   huncha  bhanera chahin  bannu sakdina,   ahile  compile  garera  debug  garna   jhyau  lagi racha,  hopefully this helps somewhat. Last edited: 10-Feb-10 02:29 AM

1987AD · Feb 10, 2010 2:26 AM

Define "char[] done" outside the for loop. And add every character shift in it inside the loop as you have done. Also do this "cipherText= new String(done);" or " cipherText = done.toString()" outside the for loop. After that you will be good to go...

hemu · Feb 10, 2010 3:53 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion