I've been doing this for the school assignment.

So the whole idea of this program is to use random numbers to disguise a message by translating it into pairs of numbers; or to reveals messages by translating a series of pairs of numbers back in to the original characters. It works by using a random number generated for each character position in the original message. Also, for the encoding part, it requires First number: The seed number for the random number generator; Second number: The number of characters in the decoded message; Each subsequent number: The integer version of the original character offset by a random number(If the character position number is zero or even the random number is added to the character’s integer value. Otherwise it is subtracted.)

This is what I got so far:

``````import java.util.Scanner;
import java.util.Random;
import java.util.regex.Pattern;
public class CodingProject{

public static final int SEED_NUMBER = 150;
public static void main(String[] arges){
Scanner scan = new Scanner(System.in);

// prompt the user to choose decoding or encoding message.
System.out.println("\nE-Encoding a message \n\nD-Decoding a message");
System.out.print("Desired Action: ");
String choice = scan.nextLine();
choice = choice.toLowerCase();
Random gen = new Random (150);

//Encoding message
if(choice.equals("e")){
// prompt user to give a message to be encoded.
System.out.println("On the next line please type or paste the message to be encoded:");

String line = scan.nextLine();

int chr = line.length();
System.out.print(SEED_NUMBER + " " + chr + " ");
for(int i = 1; i <= line.length() - 1; i++){
int offset = gen.nextInt(SEED_NUMBER) + 1;
int value = (int)line.charAt(i);

if(chr == 0 || chr % 2 == 0){
int newValue = offset + value;
System.out.print(newValue + " ");
}else{
int newValue = offset - value;
System.out.print(newValue + " ");
}
} // end for loop

}else{ // Decoding message
System.out.println("On the next line please type or paste the message to be encoded:");
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
scan.useDelimiter(delimiters);
while (scan.hasNextInt()){
int k = scan.nextInt(); // this is where I got stuck,
//somehow I managed to let the user input series of pairs of numbers,
//but I don't know what to do next in order to apply the rules and convert the numbers into characters.
}
}
} // end main method
} // end class
``````

If you are using ordinary random numbers then its hard to see how you can possibly decode the resulting randomised content.
Maybe what is intended is that you use the seed value to seed the random number generator. The Java Random class guarantees that "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.".
So you set the seed, generate random numbers to encode the message, then in decoding you set the same seed value, so you can generate the same "random" sequence again and use that to decode.

If you are using ordinary random numbers then its hard to see how you can possibly decode the resulting randomised content.
Maybe what is intended is that you use the seed value to seed the random number generator. The Java Random class guarantees that "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.".
So you set the seed, generate random numbers to encode the message, then in decoding you set the same seed value, so you can generate the same "random" sequence again and use that to decode.

If you are using ordinary random numbers then its hard to see how you can possibly decode the resulting randomised content.
Maybe what is intended is that you use the seed value to seed the random number generator. The Java Random class guarantees that "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.".
So you set the seed, generate random numbers to encode the message, then in decoding you set the same seed value, so you can generate the same "random" sequence again and use that to decode.

Thank you for the answer! However I still don't know how to actually let the program scan each pairs of numbers in the decoding section. Do i need to use for loop for that? If so, how?

Does the code read in the numbers OK? Print them out to see what is being read in.

Have you tried to manually encode a single letter and decode it to verify that you understand the algorithm?

algorithm

you tried to manually encode a single letter and decode it to verify that you understand the algorithm?

you tried to manually encode a single letter and decode it to verify that you understand the algorithm?

The decoding code reads in the numbers ok, however I am kinda vague on how to decode the number back to the character, I understand the whole idea of seeded random number, but I don't know how to make the program to do the algorithm for each set of numbers. here is the example of how is the decode code supposed to do:

E - Encode a message
D - Decode a message
Desired action: E
50 12 89 13 201 96 162 -29 123 53 130 100 100 21
Thank you. Your encoded message is:
Hello World!

where the first number is the seed number, the second number is the number of characters of the message, and each subsequent ones are The integer version of the original character offset by a random number(as I've already explained).

Have you done the encode/decoding manually for a few letters so you know you have the correct algorithm?

If you work through the encode step, you should get some insight on how to do the decode.

Ok, the instruction says "for each character position in the original message that generator is used to generate a random number between 0 and 99. If the character position number is zero or even the random number is added to the character’s integer value." Does that mean that I need to put the random generator inside the for loop, so that I will have different offset number for each character position, or that I interpreted it wrong?

Have you done the encode/decoding manually for a few letters so you know you have the correct algorithm?

If you work through the encode step, you should get some insight on how to do the decode.

I've worked on paper for the algorithm, and I got it, however I am stuck on the place where I don't know how to use the code in order to interact each set of the numbers inputted by the user for the decode.

Sorry, I just started learning Programming...

interact each set of the numbers

What are the set of numbers you talk about?

Can you post the steps taken when you manually encode a letter?
Say encode 'A' what is the result?
Then try decoding that result and get the letter 'A' back.

What are the set of numbers you talk about?

Can you post the steps taken when you manually encode a letter?
Say encode 'A' what is the result?
Then try decoding that result and get the letter 'A' back.

I'm talking about the example I showed above, where the program asks the user to input series of paires of numbers.

In order to encode 'A':
1. first number in the encoded message would be the seed number (let's say 150).
2. The second number would be the number of characters in the message inputted by the user.
3. For the subsequent number in this case would only have one since there is only one character. So use Random gen = new Random(100); to generate a "random" number between 0 and 99.
4. Since the character position for 'A' is 0, add the offset "random" number(let's say it generates 20) to the int version of 'A'(65), and the number is 20 + 65 = 85.

The encoded message is: 150 1 85

For decode:
1. Ignore the first two number because they are essentially the seed number and the number of characters.
2. Subtract the "random" number generated in the encode code from 85 to get 65.
3. cast (char) before the number in order to get the character.

The steps are what I would do manually encode/decode message.
Please correct me if I have anything wrong. Thank you.

The encoded message is: 150 1 85

That looks reasonable.
Does the program generate that same number as you got for 'A'?
Execute the code and print out the random numbers that are generated. What are the values of the variable offset that are generated?

Does the program generate that same number as you got for 'A'?
Execute the code and print out the random numbers that are generated. What are the values of the variable offset that are generated?

I got the output: 150 1 144 79 when I run the program for encode, where the fourth number is the offset number that is generated by the code.

P.S. I ran the code several times and the outputs are the same.

Strange you get a different value for offset. I got 132 for the first value and 130 for the second.

Now work on the decode part.

Strange you get a different value for offset. I got 132 for the first value and 130 for the second.

Now work on the decode part.

That's probably because I put the "int offset = gen.Random(100);" outside of the for loop.

You shouldn't change the values when testing. The posted code uses 150.

You shouldn't change the values when testing. The posted code uses 150.

Sorry, I relized that the random number generated is not supposed to be the seed number itself. It's supposed to be a number between 0-99, so I used "int offset = gen.Random(100);", I still have the "Random gen = new Random(150);" since 150 is my seed number.

``````import java.util.Scanner;
import java.util.Random;
import java.util.regex.Pattern;
public class CodingProjectTest{

public static final int SEED_NUMBER = 150;
public static void main(String[] arges){
Scanner scan = new Scanner(System.in);

// prompt the user to choose decoding or encoding message.
System.out.println("\nE-Encoding a message \n\nD-Decoding a message");
System.out.print("Desired Action: ");
String choice = scan.nextLine();
choice = choice.toLowerCase();
Random gen = new Random (SEED_NUMBER);
//Encoding message
if(choice.equals("e")){
System.out.println("On the next line please type or paste the message to be encoded:");
String line = scan.nextLine();
int chr = line.length();
System.out.println("Thank you! Your encoded message is:");
System.out.print(SEED_NUMBER + " " + chr + " ");
int offset = gen.nextInt(100);
for(int i = 0; i < line.length(); i++){
int value = (int)line.charAt(i);
if(i == 0 || i % 2 == 0){
int newValue = offset + value;
System.out.print(newValue + " ");
}else{
int newValue = offset - value;
System.out.print(newValue + " ");
}
} // end for loop
}else{ // Decoding message
int offset = gen.nextInt(100);
System.out.println("On the next line please type or paste the message to be encoded:");
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
scan.useDelimiter(delimiters);
while (scan.hasNextInt()){
int k = scan.nextInt();
if(k == SEED_NUMBER || k == 12){
}else if(k == 0 || k % 2 == 0){
int value1 = k - offset;
char Newvalue = (char)(value1);
System.out.print(Newvalue + " ");
}else{
int value1 = offset - k;
char Newvalue = (char)(value1);
System.out.print(Newvalue + " ");
}
}
}
} // end main method
} // end class
``````

I changed something in my code and now it is pretty close to the goal. For the decode, it prints out: ? ? ? l o W ? ? l ? ? . (my message was: Hello World!)

Print out the int values of value1 to make sure you are computing them correctly.

The decode section does not look like it goes with the encode section. Have you worked through the logic manually to verify that it is correct?

No, unfortunately it doesn't work...

``````else{ // Decoding message
int offset = gen.nextInt(100);
System.out.println("On the next line please type or paste the message to be encoded:");
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
scan.useDelimiter(delimiters);
while (scan.hasNextInt()){
int k = scan.nextInt();
if(k == SEED_NUMBER || k == 12){ // I tried to use "line.length();", but it's in the if statement, so it can't idenify it.
}else if(k == 0 || k % 2 == 0){ // I need to somehow relate this to the decoded message.
int value1 = k - offset;
char Newvalue = (char)(value1);
System.out.print(Newvalue + " ");
}else{
int value1 = offset - k;
char Newvalue = (char)(value1);
System.out.print(Newvalue + " ");
}
}
}
``````

That's because it is not undoing what the encode section does. Look at that code again and see what it does: Takes the letters one at a time and gets a random number and either adds or subtracts depending on the letter's position. The decode needs to do same sort of thing but the reverse of adding and subtracting. Work through the encoding of a couple of letters and then take the encoded results for those letters and work out the decoding.

Remember to read the first two numbers as seed and count before trying to decode what follows.