# Cypher 1 and 2 exercises

5 posts / 0 new
odonoe22
Cypher 1 and 2 exercises

Hi All

Wonder can anyone give me at least whether im going in the right direction and maybe give me advice re the lab exercises Cypher 1 & 2.

I get the idea of using substrings and lengths to move the string input around but to truly muddle the input around my guess is that i need to create a String Array and use a For Loop to assign each character in the inputted String to a sequential string array variable using a sequential count on the String array variable and the charAt method. Then my guess is i can use the random class, using the length method to define the range in the random range, to output the inputted original in random order, which i propose doing using a While Loop.

However the difficulty is as i see it will be to prevent duplicate randoms occurring and therefore you get the same string character twice. I think (?) i know how to prevent duplicate randoms using While Loops which is ok for a small number of randoms such as in the Lotto program but if the inputted string is lengthy then this would be tricky.

So my first question is there a neat way to prevent duplicate randoms, my second question is whether this is a valid approach or have i gone on a complete tangent and missed some far more straightforward way of doing this?? Keeping Cypher2 in mind i see no other way than using the random class??

Thanks in advance for any hints

Eoin

cromiem2
Hey Eoin This works for me,

Hey Eoin

This works for me, but may not be the best way to go about it.  If theres a better way I'd love to know as well.

It actually doesn't work for the cypher 2 exercise as I can only generate the index values of each character as it currently is at that time when the programme is running not as it was originally inputted

Regards

Mark

public class Cypher1
{

public static void main (String []args)
{
Scanner sc = new Scanner(System.in);
String message = sc.nextLine(); //create string from user input
String message1 = "";//create temporary string to hold substring
String message2 = "";//create temporary string to hold substring
String jmessage = ""; //create string to hold jumbled version of input

while (0 < message.length()) //while message length is greater than 0 carry out the following
{
int len = message.length(); //length of input
Random r =  new Random();
int random = r.nextInt(len);//create random number to be used to pick random character in input
char randomLetter = message.charAt(random); //picks random character from input

message1 = message.substring(0,random);//creates substring of input from start to random
message2 = message.substring((++random),len);//creates substring of input from +1 random to end
message = message1+message2;//re-creates message without random character
jmessage = jmessage+randomLetter;//adds random character to jumbled string

message = message.substring(0,len-1); //create substring of message and removes 1 from length

}

System.out.print (jmessage); //print out new string of jumbled letters
}

}
jpendlebury
Over complicated

Hi,

This is good but a little over complicated.

There is no need to use charAt().

Have you looked at the hangman program that I put on moodle. This allows the user to enter the index of a character to remove from a string.

Instead of the user  choosing the index of a character to remove from the string, we would generate that index with random.

Now imagine you create an empty destination string at the start

String dest = "";

Then all you have to do when you remove the random character is to concatenate the substring you remove from the original message with dest.

dest = dest +removed.

Remember removed is a String not a char.

John

armstrp2
I have cypher 1 working but

I have cypher 1 working but having trouble with the index part for cypher 2. I am printing out the index for the string but it's the index of the changed string -  after a substring character has been removed. I could change the code to pick all the random numbers before removing any of the characters but that would mean I couldn't use a loop.

any thoughts?

armstrp2
actually after talking to

actually after talking to John it seems I have the output I need. now to work on the decoding.