473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help finishing code for a encoding program

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
0 1906

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

Similar topics

3
by: JGBNS via DotNetMonster.com | last post by:
Hi, I am new to this forumand I apologize as i am not a .net programmer but we have a program being developed by a .net programmer. Nowwe have run into an ftp snag and I think it is part ftp and...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
11
by: miss_Lolitta | last post by:
Hi all,, I am hoping that you will help me. I have trouble how I can complete the code of program that allows the user to input a number of integers such as N, and then stores them in an integer...
2
by: shblack | last post by:
Please can someone help me with this program. I am in a JAVA programming class and I am having a heck of a time. I am still having a problem with the basic concepts of JAVA but the teacher continues...
1
by: TwistedSpanner | last post by:
Hello all, For the record I am a complete java novice. I have to write a program to generate/output to screen 10 simple maths question and output a final score . The question is as follows ...
3
by: shubhamgupta | last post by:
somebody plz help me with a program for inputing two numbers and then checking if they are amicable or not. i've just started learnign c++ and we are being made to use turbo c++ 3.0 we have been...
6
by: isabelle | last post by:
help me in this program.... Write a program that prompt the user ti input an integer and then outputs the numbers with digits reversed. for example, if the input is 12345 the output should be...
1
by: ImortalSorrow | last post by:
Please I need urgent help with this program! Everything is working well except a part where i want to repeat the riddle if the answer isnt correct. here's the code..hope someone can help me! ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.