wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with an encoding/decoding program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with an encoding/decoding program" Watch "Help with an encoding/decoding program" New topic
Author

Help with an encoding/decoding program

Null Nullington
Greenhorn

Joined: Sep 13, 2007
Posts: 2
I'm currently enrolled in a Computer Science class in high school, and we were recently assigned a program which is a modified version of a program in David J. Eck's online text. The original program is available here: http://math.hws.edu/javanotes/c4/ex1-ans.html

The assignment the class was given was to modify the program so that it asks the user to enter a string, code word, and whether to encode or decode the phrase. The program uses the simple encoding technique of matching up the numerical value of a character and adding it to the original string. Following is the code I have worked and attempted to debug over the past couple days:



There are two main problems I am experiencing. Our specifications say to test the program using the message "Taxi Cab" and the code word "cafe". When adding the numerical value 6 (from the 'f' in cafe) to the string, my program does not wrap around to the beginning of the alphabet, and simply keeps going through the unicode character set. The second problem is that although I use a counter variable to determine which part of the code word I am adding to the string, the counter doesn't seem to reset itself properly.

Any input or advice that anyone is willing to contribute to my program would be greatly appreciated. Also, please excuse my amateur style

Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
For your first problem, there are a couple of solutions, but you're going to have to be intelligent about your use of capitals.

The first option is to check to see if the character code is larger than 'z'. If it is, subtract 26 from it. BUT notice that Z + B doesn't give you a letter, but it also isn't greater than 'z'.

The second option is to use the modulo operation. Subtract 'A' from both the input and the keyword, so that all of the characters are between 0 and 25. Then, when you get your result, do this: result = result % 26. Then, add 'A' back into the picture. This will give you the wrap around that you want. You need to be careful about capitals, again, here, though. If you have a capital letter you want to subtract and re-add 'A', if you have a lowercase letter, you want to subtract and re-add 'a'.

- Adam
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
With regard to your second question, I would wait to see if the fix to your first problem gets things working for you. I'm not entirely sure that your counter doesn't reset itself. It may have to do with forgetting to deal with capitals.

- Adam
Null Nullington
Greenhorn

Joined: Sep 13, 2007
Posts: 2
Thanks a ton for your help Adam! I'm working right now on the changes you suggested and I'll let you know how it goes.

Thanks once again!
-Ben
 
jQuery in Action, 2nd edition
 
subject: Help with an encoding/decoding program
 
Similar Threads
Index of the last character of Strings
How to convert a String to char
analyze shape
charAt() method ?? :from one of the mock exams.
query