Caesar Cipher in Java

Writing a Caesar Cipher program in Java using loops and typecasting

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 61674 Views - Last Post: 14 July 2010 - 08:50 AM Rate Topic: -----

#1 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Caesar Cipher in Java

Posted 09 July 2010 - 10:46 AM

Hello,

I'm new to Java programming entirely and I'm looking for some useful pointers and help with regard to writing a Caesar Cipher program.

Goal:
Write a program that uses the Caesar cipher to encode and decode text. Students should be able
to use loops and type casting to perform this task.


Description:
Write a java program called CaesarCipher.java that does the following. Your program will
present the user with a menu to encode or decode text, then get the text and the key from the user and
use these to encode or decode the text. See “Relevant Knowledge” for how the Caesar cipher works. A
sample run of the program is shown below:

D:\Dev\CSCI1301\Project2>java CaesarCipher
1 for encode in Caesar cipher
2 for decode from Caesar cipher
What do you want to do: 1
What is the text you want to encode: i came i saw i conquered
What is the key:1
Encoded Text:
j dbnf j tbx j dporvfsfe

For this program only the lowercase letters and spaces need to be handled (you can do anything
at all with upper case and punctuation). Be sure that your program can encode text then decode that
same text.

I really need the help with regard to constructing the loops...I'm continually trying but everything I have keeps giving my errors. Here is what I have so far...it's just the beginning but any pointers to teach me how to create the loops would be most appreciated.


Thank you all!
Wolfmanzak

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Caesar Cipher in Java

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 10:49 AM

Here is a tutorial on loops:

http://leepoint.net/...oops/loops.html

What specifically do you need help on with the loops? THe conditions, etc?
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Caesar Cipher in Java

Posted 09 July 2010 - 10:51 AM

Please post your code in the body of your post using code tags, like so: :code:. Also, please specifically describe any problems or errors you are encountering.

I've written a Caesar Cipher Encode snippet if you want to check it out.
Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 10:53 AM

Here's his code, hes not exactly finished; it was in the text attachment:

import java.util.Scanner;

public class CaesarCipher
  
{
  public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    
    System.out.println("1 for encode in Caesar cipher ");
    System.out.println("2 for decode in Caesar cipher ");
    
    System.out.println("What do you want to do: ");
    int decision = keyboard.nextInt();
    
    if (decision == 1)
      System.out.println("What is the text you want to encode?");
    else if (decision == 2)
      System.out.println("What is the text you wish to decode?");
    else
      System.out.println("Please choose either 1 for encode or 2 for decode, in the Caesar cipher program.");
    
    String input = keyboard.nextLine();
    
    System.out.print("What is the key: "); 
    int key = keyboard.nextInt();


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Caesar Cipher in Java

Posted 09 July 2010 - 11:00 AM

The only things I'll note are that you are missing to cause syntax errors right now are the end braces for your main() method and class.

@bcranger: Just a heads up that we have been attacked by attachments in the past. When dealing with members you don't know, open them at your own risk. (@Wolfmanzak: which is why we ask users to include their code in the body of the post) :)
Was This Post Helpful? 0
  • +
  • -

#6 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 11:03 AM

ahhh lol :eek:

since its registered as a .txt file, shouldn't it be ok? I thought batch files were the most troublesome
Was This Post Helpful? 0
  • +
  • -

#7 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 11:43 AM

View Postbcranger, on 09 July 2010 - 09:49 AM, said:

Here is a tutorial on loops:

http://leepoint.net/...oops/loops.html

What specifically do you need help on with the loops? THe conditions, etc?



Some help on the conditions of a loop would be great!!! I understand the concept of a loop, with it continuing so long as a condition is still true, but I'm a bit confused on how to set up the conditions for the loops. Correct me if I'm wrong, but I see how the loops resemble a "if" statement...basically, you're given a condition and so long as it is true...the "if" function executes a defined command.

And I'm looking at your tutorial right now...thank you for the post. It's a great help.
Was This Post Helpful? 0
  • +
  • -

#8 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 11:48 AM

View Postmacosxnerd101, on 09 July 2010 - 10:00 AM, said:

The only things I'll note are that you are missing to cause syntax errors right now are the end braces for your main() method and class.

@bcranger: Just a heads up that we have been attacked by attachments in the past. When dealing with members you don 't know, open them at your own risk. (@Wolfmanzak: which is why we ask users to include their code in the body of the post) :)



Sorry about having it as an attachment, I'm new to the site. I was just basically going with what I saw.

Here is my code: I guess my first question is, how should I take user input such as a "key" for the Cipher and the actual text to be ciphered and combine them in a loop so that the loop takes the entire text input and moves the letters down the alphabet by the amount of letters specified in the "key?"


BTW, this website ROCKS!!! I never expected to have such great replies in such a short amount of time. Thanks again!


import java.util.Scanner;

public class CaesarCipher

{
	public static void main (String [] args) {
		Scanner keyboard = new Scanner(System.in);
	
		System.out.println("1 for encode in Caesar cipher ");
		System.out.println("2 for decode in Caesar cipher ");
	
		System.out.println("What do you want to do: ");
		int decision = keyboard.nextInt();
	
		if (decision == 1)
			System.out.println("What is the text you want to encode?");
		else if (decision == 2)
			System.out.println("What is the text you wish to decode?");
		else
			System.out.println("Please choose either 1 for encode or 2 for decode, in the Caesar cipher program.");
	
		String input = keyboard.nextLine();
	
		System.out.print("What is the key: ");	
		int key = keyboard.nextInt();
	
	
		
	}
}



Edited by macosxnerd101: Please remember to post your code using code tags, like so: :code:. :)

This post has been edited by macosxnerd101: 09 July 2010 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

#9 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 12:00 PM

Glad to help; don't forget to look up Mac's link if you get stuck.

And your exactly right, the body of a loop statement is equivalent to the body of an If statement; you are simply toying with the conditions in a more manageable way :)
Was This Post Helpful? 0
  • +
  • -

#10 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Caesar Cipher in Java

Posted 09 July 2010 - 12:08 PM

As for moving/shifting your characters, I would suggest using the char class and some of its methods:

- toCharArray() will put your text into a character array which you can work with
- you can easily manipulate the characters inside the array; for example, to shift
letter "a" to letter "n" you would add 13 to the element, as characters have int
values that represent each character

Could you elaborate on what you wanted to do with the loop? Do you want to prompt the user for all the input, and if the input is invalid, prompt for input again?

Glad to see you like DIC :bananaman:
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Caesar Cipher in Java

Posted 09 July 2010 - 12:19 PM

@Wolfmanzak: Take a look at the ASCII Table. Each char has a corresponding int value, which you can increment. In Java, the following is also valid: char c = 'a'+3;. In this case, c == 'd'.

@bcranger: What the Caesar Cipher does is it takes a String and shifts each character three digits down in the alphabet. So if you encode abc with the Caesar Cipher, the output will be def.
Was This Post Helpful? 0
  • +
  • -

#12 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Caesar Cipher in Java

Posted 09 July 2010 - 12:19 PM

Quote

I guess my first question is, how should I take user input such as a "key" for the Cipher and the actual text


the "key" is what you need to add to your character to "bump" the value, "a" would be displayed as "c" if the key is 2.
input is your String that the user entered.

build your forloop like
for(int i=0;i<input.length();i++)

this will drive thru each character one at a time.

EDIT: Gotcha, thanks mac

This post has been edited by NoobKnight: 09 July 2010 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Caesar Cipher in Java

Posted 09 July 2010 - 12:21 PM

Don't forget the end parenthesis on your loop. :)
Was This Post Helpful? 0
  • +
  • -

#14 Wolfmanzak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-July 10

Re: Caesar Cipher in Java

Posted 12 July 2010 - 06:08 AM

View Postbcranger, on 09 July 2010 - 11:08 AM, said:

As for moving/shifting your characters, I would suggest using the char class and some of its methods:

- toCharArray() will put your text into a character array which you can work with
- you can easily manipulate the characters inside the array; for example, to shift
letter "a" to letter "n" you would add 13 to the element, as characters have int
values that represent each character

Could you elaborate on what you wanted to do with the loop? Do you want to prompt the user for all the input, and if the input is invalid, prompt for input again?

Glad to see you like DIC :bananaman:




I want the loop to prompt the user to decide whether they want to encode or decode using the cipher, and what key they want to use in the code. After making these decisions, I want the program to then ask for the input text that is to either be encoded or decoded. If they give an invalid input, then I want the program to give a small error message and then prompt for the correct input again.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Caesar Cipher in Java

Posted 12 July 2010 - 06:09 AM

Awesome. I can't wait to see your efforts. :)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3