By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

Help finishing code for a encoding program

P: 1
I need some help! Here is my assignment.
Alphabet Run Encryption - For this challenge you will be decoding a message.



Using the Java language, create the function AlphabetRunEncryption(str) read the str parameter being passed which will be an encrypted string and your program should output the original decrypted string. The encryption being used is the following: For every character i in str up to the second to last character, take the i and i+1 characters and encode them by writing the letters of the alphabet, in order, that range in the same direction between those chosen characters.



For example: if the original string were bo then it would be encoded as cdefghijklmn, but if the string were boa then bo is encoded as cdefghijklmn and oa is encoded as nmlkjihgfedcb with the final encrypted string being cdefghijklmnnmlkjihgfedcb. So str may be something like the encrypted string just written, and your program should decrypt it and output the original message. The input string will only contain lowercase characters (a...z). There are also three important rules to this encryption based on specific character sequences. 1) If the original string contains only one letter between two chosen characters, such as the string ac then this would be encrypted as bR with R standing for what direction in the alphabet to go in determining the original characters. The encrypted string bR represents ac but the encrypted string bL represents ca (R = right, L = left). 2) If the original string contains zero letters between two chosen characters, such as the string ab then this would be encrypted as abS, with S representing the fact that no decryption is needed on the two letters preceding S. For example, if the original string were aba then the encryption would be abSbaS, but be careful because decrypting this you get abba, but the actual original string is aba. 3) If the original string contains a repeat of letters, such as the string acc then this would be encrypted as bRcN, with N representing the fact that no change in characters occurred on the character preceding N. The input string will never only be composed of repeated characters.



Correct Sample Output

Input = "bcdefghijklmnopqrstN"Output = "att"
Input = "abSbaSaNbR"Output = "abaac"

Here is my code....
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * Create a Decoder.
  3.  * 
  4.  * 
  5.  */
  6. package finalproject;
  7.  
  8.  
  9. public class FinalProject {
  10.  
  11.  
  12.     public static void main(String[] args) {
  13.         // Creating the test strings
  14.  
  15.         //final should be "att"
  16.         String testi = new String("bcdefghijklmnopqrstN");
  17.  
  18.         //final should be "abaac"
  19.         String testii = new String("abSbaSaNbR");
  20.  
  21.         //not sure if this will be relevant
  22.         char[] arrayi = testi.toCharArray();
  23.         int[] asciI = new int[arrayi.length];
  24.         char[] arrayii = testii.toCharArray();
  25.         int[] asciIi= new int[arrayii.length];
  26.  
  27.         String translated = new String(); //using translated += temporary, adds letters one at a time.
  28.  
  29.         for (int i = 0; i < arrayi.length; i++){
  30.             char temporary = 'A'; //declares a char
  31.             // was used to test
  32.             //System.out.print(arrayi[i]);
  33.             char temp = arrayi[i];
  34.  
  35.             asciI[i] = ASCIItranslate(temp);
  36.             if(i == 0){ //if its the first letter instantly go back a letter, to start off the decoder
  37.                 if (asciI[i]!= 'a'){
  38.                     temporary = (char)(asciI[i] - 1);
  39.                     translated += temporary;
  40.                 }
  41.                 else{
  42.                     temporary = (char)(asciI[i]);
  43.                     translated += temporary;
  44.                 }
  45.             }
  46.                 else{ // if its not the first letter it checks for the rules.
  47.  
  48.                  // 'S'
  49.                     if (asciI[i] == 'S' ){
  50.                         //do this
  51.                         translated += temporary;
  52.                     }// 'N'
  53.                     if(asciI[i] == 'N' ){
  54.                         removeLastChar(translated);
  55.                         temporary = (char)(asciI[i-1]);
  56.                         translated += temporary;
  57.                         translated += temporary;
  58.                         }
  59.                     if(asciI[i]== 'L'){
  60.                         temporary = (char)((asciI[i-1])-1);
  61.                         translated += temporary;
  62.                     }
  63.                     if (asciI[i]== 'R'){
  64.                         temporary = (char)((asciI[i-1])+1);
  65.                         translated += temporary;
  66.                     }
  67.                 }
  68.  
  69.         }
  70.             System.out.println(translated);
  71.     }
  72.  
  73.     //translates to ASCII which uses integers
  74.     public static int ASCIItranslate(char letter) {
  75.         return letter;
  76. }
  77.  
  78.     //removes the last char
  79.     private static String removeLastChar(String str) {
  80.         return str.substring(0,str.length()-1);
  81.     }
  82. }
  83.  

My problem is that when I run it with the selected


Expand|Select|Wrap|Line Numbers
  1. String testi = new String("bcdefghijklmnopqrstN");
I get the correct output of att, however when I switch it and run it as

Expand|Select|Wrap|Line Numbers
  1. String testii = new String("abSbaSaNbR");

instead of the output being abaac i get abaaac. Im about to rack my brain on this one. I know that if I change my code of


Expand|Select|Wrap|Line Numbers
  1. if(asciI[i] == 'N' ){
  2. removeLastChar(translated);
  3. temporary = (char)(asciI[i-1]);
  4. translated += temporary;
  5. translated += temporary;
to


Expand|Select|Wrap|Line Numbers
  1. if(asciI[i] == 'N' ){
  2. removeLastChar(translated);
  3. temporary = (char)(asciI[i-1]);
  4. translated += temporary;

and remove the second translation and run it by switching the test from test i to test ii, I get the correct output for the "abaac" but then I lose a t on the "att" and it only displays as "at". What am I missing or what am I doing wrong?!?! Any help is greatly appreciated!!!
Jul 24 '15 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.