10 Replies - 2573 Views - Last Post: 25 June 2013 - 09:04 PM Rate Topic: -----

#1 jcamana10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-June 13

Substitution Cipher

Posted 23 June 2013 - 12:41 PM

I have a project regarding substitution ciphers. Here are the requirements.

Provide a complete program that performs a simple substitution cipher. The program should take plain text and a shift value and produce the encrypted text. Then it should take encrypted text and a shift value and produce the plain text once again. A different encrypted text and shift can be entered so make sure to get input.

Example:
EASTER shifted by 3 to left would become HDVWHU
HDVWHU shifted by 3 to right would become EASTER

This is how the early Caesar Cipher worked.

Provide a main method. It should:

Get input for a string and a shift value
Convert to upper case
Only perform the following items on alphabetic characters between A and Z
Utilize a for loop which uses postfix incrementing operator
Convert character to its ASCII equivalent (type cast)
Shift buy shift value entered above
If you reach end of alphabet, wrap around
Example: A shifted to the left 2 would become Y
Convert back to its character equivalent (type cast)
Output the new character
Get input for a string and a shift value
Perform same steps above to convert the encrypted text back to plain text
Be sure to get input again as a different encrypted text may be entered
Utilize postfix increment/decrement operations and compound assignment operators for all math. Example: x++ or x+=2.

Sample Session:

Please enter text to encrypt
easter
Please enter shift value
3
BXPQBO
Please enter text to decrypt
bxpqbo
Please enter shift value
-3
EASTER
Press any key to continue . . .

Sample Session 2: : (Notice spaces removed from input)

Please enter text to encrypt
This Is A Test
Please enter shift value
3
QEFPFPXQBPQ
Please enter text to decrypt
qefpfpxqbpq
Please enter shift value
-3
THISISATEST
Press any key to continue . . .

Sample Session 3: (Notice the different shift and decrypt value)

Please enter text to encrypt
hello
Please enter shift value
3
EBIIL
Please enter text to decrypt
ebiil
Please enter shift value
5
ZWDDG
Press any key to continue . . .

Can someone assist with my code. I have a bunch of errors and Im lost. Here's my code

import java.lang.*;

import java.util.*;

public class JRCamanaProg3
{
public static void main(String[] args)
 {
	Scanner stdIn = new Scanner(System.in);
	String encryptText; //text to encrypt
	int shiftValue; //number of spaces to shift
	String decryptText; //text to decrypt
	
	//User enters text to encrypt
	System.out.print("Please enter text to encrypt");
	encryptText = stdIn.nextString();
	
	//User enters shift value
	System.out.println("Please enter shift value");
	shiftValue = stdIn.nextInt();
	
	//System returns encrypted string
	String encryptedText = encrypt(encryptText , shiftValue); // text that has been encrypted
	System.out.println(encryptedText);
	
	//User enters text to decrypt
	System.out.print("Please enter text to decrypt");
	decryptText = stdIn.nextString();
	
	//User enters shift value
	System.out.println("Please enter shift value");
	shiftValue = stdIn.nextInt();
	
	//System returns decrypted string
	String decryptedText = decrypt(decryptText , shiftValue); // text that has been decrypted
	System.out.println(decryptedText);
	}

	
	//Shift and manipulate characters
	public static String shift(String enteredText, int shiftValue) 
	{
		
		String convertedText = "";
		for(int i = 0; i< enteredText.length(); i++){
		char lowerCase = enteredText.charAt(i);
		
		//Convert to upper case letters
		char lowerCase = Character.toUpperCase(lowerCase);
		int charNumber = upperLetter;
		
		//Shift letters and wrap text
		int rotateLetters = (charNumber + shiftValue) % 26;
		char shiftLetters = (char) rotateShift;
		
		//Populate new string of characters
		convertedText += shiftLetters;
	}
	return convertedText;
}

	//Encryption
	public static String encrypt(String enteredText, int shiftValue);
	{
	String encryptedString = rotate(enteredText , shiftValue);
	return encryptedString;
	}
	
	//Decryption
	public static String decrypt(String enteredText, int shiftValue);
	{
	int negativeShiftValue = (-1) * (shiftValue);
	String decryptedString = rotate(enteredText , negativeShiftValue);
	return decryptedString;
	}
}// end

This post has been edited by GregBrannon: 23 June 2013 - 12:46 PM
Reason for edit:: Added code tags. Please use code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Substitution Cipher

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Substitution Cipher

Posted 23 June 2013 - 01:15 PM

You need to post your errors exactly as they're appearing. Otherwise, we won't be able to help.
Was This Post Helpful? 0
  • +
  • -

#3 jcamana10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-June 13

Re: Substitution Cipher

Posted 23 June 2013 - 01:36 PM

Sorry, Im new to this... I have multiple errors

Here are the errors:

The import java.lang is never used

The method nextString() is undefined for the type Scanner- Lines 16 and 28

Duplicate local variable lowerCase- Line 49

upperLetter cannot be resolved to a variable- Line 50

rotateShift cannot be resolved to a variable- Line 54

This method requires a body instead of a semicolon- Lines 63 and 70

Multiple markers at line 65 - enteredText cannot be resolved to a variable
- shiftValue cannot be resolved to a variable

Void methods cannot return a value- Line 66 and 74

shiftValue cannot be resolved to a variable- Line 72

enteredText cannot be resolved to a variable- Line 73
Was This Post Helpful? 0
  • +
  • -

#4 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Substitution Cipher

Posted 23 June 2013 - 01:46 PM

Quote

The import java.lang is never used


Not an error, just a warning saying that you didn't use that import.

Quote

The method nextString() is undefined for the type Scanner- Lines 16 and 28


nextString() isn't a method for Scanner. Try nextLine()

Quote

Duplicate local variable lowerCase- Line 49


You can't declare the same variable twice.

Quote

This method requires a body instead of a semicolon- Lines 63 and 70


Semicolon needs to be removed.



That's a few fixed. The errors are pretty self explainatory. As you haven't created the variables for the other variables that you called.

This post has been edited by schutzzz: 23 June 2013 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 jcamana10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-June 13

Re: Substitution Cipher

Posted 23 June 2013 - 03:01 PM

Thanks. I think I was able to compile it and run the program but the results im getting is nowhere close to being correct. What am i doing wrong? here's my updated code:
import java.util.*;

public class JRCamanaProg3
{
public static void main(String[] args)
 {
	Scanner stdIn = new Scanner(System.in);
	String encryptText; //text to encrypt
	int shiftValue; //number of spaces to shift
	String decryptText; //text to decrypt
	
	//User enters text to encrypt
	System.out.println("Please enter text to encrypt");
	encryptText = stdIn.nextLine();
	
	//User enters shift value
	System.out.println("Please enter shift value");
	shiftValue = stdIn.nextInt();
	
	//System returns encrypted string
	String encryptedText = encrypt(encryptText , shiftValue); // text that has been encrypted
	System.out.println(encryptedText);
	
	//User enters text to decrypt
	System.out.println("Please enter text to decrypt");
	decryptText = stdIn.nextLine();
	
	//User enters shift value
	System.out.println("Please enter shift value");
	shiftValue = stdIn.nextInt();
	
	//System returns decrypted string
	String decryptedText = decrypt(decryptText , shiftValue); // text that has been decrypted
	System.out.println(decryptedText);
	}

	
	//Shift and manipulate characters
	public static String shift(String enteredText, int shiftValue) 
	{
		
		String convertedText = "";
		for(int i = 0; i< enteredText.length(); i++){
		char lowerCase = enteredText.charAt(i);
		
		//Convert to upper case letters
		lowerCase = Character.toUpperCase(lowerCase);
		int upperLetter = 0;
		int charNumber = upperLetter;
		
		//Shift letters and wrap text
		int rotateLetters = (charNumber + shiftValue) % 26;
		char rotateShift = 0;
		char shiftLetters = (char) rotateShift;
		
		//Populate new string of characters
		convertedText += shiftLetters;
	}
	return convertedText;
}

	//Encryption
	public static String encrypt(String enteredText, int shiftValue)
	{
	    String encryptedString = rotate(enteredText , shiftValue);
	    return encryptedString;
	}
	
	private static String rotate(String enteredText, int shiftValue) 
	{
		String encryptedString = rotate(enteredText , shiftValue);
		return encryptedString;
	}


	//Decryption
	public static String decrypt(String enteredText, int shiftValue)
	{
		int negativeShiftValue = (-1) * (shiftValue);
	    String decryptedString = rotate(enteredText , negativeShiftValue);
	    return decryptedString;
	}
	
	}// end


This post has been edited by macosxnerd101: 23 June 2013 - 04:26 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Substitution Cipher

Posted 23 June 2013 - 04:27 PM

A couple things. First, you need to use code tags: :code:.

Second, in order for us to help you, it is important that you define exactly how your program "isn't working" in comparison to the expected results. The more specific you can be, the better help we can provide. Telling us that "it isn't working" isn't good enough.
Was This Post Helpful? 0
  • +
  • -

#7 jcamana10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-June 13

Re: Substitution Cipher

Posted 23 June 2013 - 04:36 PM

I apologize.

The issue that I am having is when I run my program, this is the result that I get:

Please enter text to encrypt
EASTER
Please enter shift value
3
Exception in thread "main" java.lang.StackOverflowError
at JRCamanaProg3.rotate(JRCamanaProg3.java:78)
at JRCamanaProg3.rotate(JRCamanaProg3.java:78)
at JRCamanaProg3.rotate(JRCamanaProg3.java:78)...

It is not encrypting the text that I want to encrypt.
Was This Post Helpful? 0
  • +
  • -

#8 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: Substitution Cipher

Posted 24 June 2013 - 11:22 AM

I literally read through the tutorial on cipher codes the other day.

http://www.dreaminco...-ceasar-cipher/

That might help you understand your position a little better.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Substitution Cipher

Posted 24 June 2013 - 11:23 AM

Look at your rotate() method. Why are you rotating endlessly in rotate()?
private static String rotate(String enteredText, int shiftValue)
70	    {
71	        String encryptedString = rotate(enteredText , shiftValue);
72	        return encryptedString;
73	    }


Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Substitution Cipher

Posted 24 June 2013 - 11:28 AM

You don't actually have to import java.lang. You get that for free. (it's where Strings and Integers and System come from - it's imported by default)

Quote

Look at your rotate() method. Why are you rotating endlessly in rotate()?


Another way to look at this: under what circumstances could you ever stop rotate()ing?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Substitution Cipher

Posted 25 June 2013 - 09:04 PM

The easiest way is to make a double alphabet

String alpha = "abcdefghijklmnopqrstuvwzyzabcdefghijklmnopqrstuvwzyz";

to cypher with a shift of N

char cypher(char theCharacter, int shift) {
    for(int i = 0; i < 26; ++i) {
      if(alpha.charAt(i) == theCharacter)
         return alpha.charAtI(i + shift);
    }
    return theCharacter;   .. character that I don't encode
{


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1