File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Caesar cipher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Caesar cipher" Watch "Caesar cipher" New topic
Author

Caesar cipher

A Issa
Greenhorn

Joined: Mar 26, 2011
Posts: 5

Hello this is my first time using a forum for help. I am doing a java program to create a caesar cipher. I am getting an error of (This method must return a result of type String) on the public static string encrypt(String text, String theKey) and another error where (text cannot be resolved to a variable).

This is the instruction: Develop code to encrypt a string given the following encryption key "DEFGHIJKLMNOPQRSTUVWXYZABC". The encryption key is positional, so that the letter A is replaced by D, B is replaced by E, etc. We will ignore case in our substitutions.

To implement Stage 1, you should write a static method named encrypt :

public static String encrypt(String text, String theKey)

This method takes in the string to encrypt (named text) and the key and returns the encrypted string.

If you are confused about how to proceed, then here are some steps you can take:

define a class constant outside the method :
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
Inside the method, create a new string which is a lower case version of the parameter "text". We will replace the lower case characters with upper case key characters from the key so as to avoid the problem of replacing a character twice.
Create a loop that considers each character of the key (loop 26 times). Inside the loop :
extract the ith character from the key using the string method charAt.
extract the ith character from ALPHABET
call the replace method of the lower case text, changing the ith character of the alphabet to the ith character of the key.


And this is what I have so far for the code
[B]

Please help,
Thanks
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Well the error message says it all. You must return a String value in your method. For example, I added return encryptedString; at the end of your method as shown below. You will, of course, have to modify the code so that it is correct. The code the way you have written it will always return an empty String.
A Issa
Greenhorn

Joined: Mar 26, 2011
Posts: 5

Thanks the return resolved the problem. And you are right I am returning an empty string. Am I missing an if...else statement??
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

A Issa wrote:Thanks the return resolved the problem. And you are right I am returning an empty string. Am I missing an if...else statement??


No, you don't need an if statement.
Consider what you are encrypting and what the for loop does.

Regards.


String knock = "\u042F \u0418\u0433\u043e\u0440\u044c";
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

Check this out.
Latin Upper Case letters are a nice touch for Caesar cipher.

> run Caesarcipher
L,FODXGLXV


Sebolela skull
Greenhorn

Joined: Nov 23, 2014
Posts: 1
Hi, i am new to this caesar cipher encryption and clear do not understand it. Can you please make a step-by-step on how to encrypt and decrypt from a text file?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Sebolela skull wrote:Can you please make a step-by-step on how to encrypt and decrypt from a text file?

Try this


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Caesar cipher