Making a secret message reader

Hello everyone,
right now I am working on a code which accepts number of letters from users and convert it in encrypted message. but I had a problem while declaring 8=1. how can I declare 8=1.

Right now it’s not super clear what you’re asking. You might be looking for a HashMap that maps from one character to another? Or maybe a function that does the mapping based on character codes?

As i said in your last question, please Post some code to make it clear what you need and where your Problem lies. Also remember to Format it with </>.

import javax.swing.JOptionPane;
int enter = int(JOptionPane.showInputDialog(
"Please Enter Your Message!"));
String reader=str(enter);

this code above reads the string entered. now I want to program it in a way that if user enter
(“ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”).
it should print
(“#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH”)

by using String.length() and String.char.At()

Cool, and which part of this are you stuck on? Sounds like you know what you need to do.

I know what I need to do. but, I don’t know how to do :thinking:
in this code I want to declare 8=1. means if user enters 1(in JOption.Pane) it should print 8 as an output on the console. so how can I declare that value of 8=1. is there any short method?

The symbols you are using to try to express the concept of “if the users enters 1, then I want that to be encoded to 8” are highly misleading. To us programmers, the single equals sign (=), is an assignment operator, used to give a variable a new value:

int my_variable = 8;

Of course, for constants, like 8, using an assignment makes no sense. 8 is always 8; you can’t assign it the value of 1!

This aside comment is brought to you by the letter H, then number 7, and the desire to explain hilarious confusion.

But anyway…


I suggest you write the following four functions:

char encode_single_letter( char input ){
  return '~'; // TODO: write this function.
}

char decode_single_letter( char input ){
  return '~'; // TODO: write this function.
}

String encode_string( String input ){
  return "ERROR! FUNCTION NOT WRITTEN!"; // TODO: Write this function.
}

String decode_string( String input ){
  return "ERROR! FUNCTION NOT WRITTEN!"; // TODO: Write this function.
}

Of course, if you’re going to use functions, you should also put your existing code inside the standard setup() and draw() functions:

import javax.swing.JOptionPane;
String user_string;

void setup(){
  size(600,400);
  textSize(22);
  user_string = JOptionPane.showInputDialog("Please Enter Your Message!");
}

void draw(){
  background(0);
  fill(255);
  text(user_string, 20, 40);
}

I also made this draw the entered text in the sketch window to keep it from being boring.


So anyway, you’re trying to fill in those four functions. Let’s start with the one to encode a letter.

If the letter we want to encode is ‘1’, then the output we want is ‘8’. Hey, that sentence sounds like code!

if( input == '1' ){
  return( '8' );
}

Of course this is not the only letter we might need to encode.

We might get a ‘Z’. That means the output should be ‘H’.

if( input == 'Z' ){
  return( 'H' );
}

Before we get confused, let’s put everything we have together so far:

import javax.swing.JOptionPane;
String user_string;
//int my_variable = 8; // Wait, did we need this?

void setup() {
  size(600, 400);
  textSize(22);
  user_string = JOptionPane.showInputDialog("Please Enter Your Message!");
}

void draw() {
  background(0);
  fill(255);
  text(user_string, 20, 40);
}

char encode_single_letter( char input ) {
  if ( input == '1' ) {
    return( '8' );
  }
  if ( input == 'Z' ) {
    return( 'H' );
  }
  return '~'; // TODO: write this function.
}

char decode_single_letter( char input ) {
  return '~'; // TODO: write this function.
}

String encode_string( String input ) {
  return "ERROR! FUNCTION NOT WRITTEN!"; // TODO: Write this function.
}

String decode_string( String input ) {
  return "ERROR! FUNCTION NOT WRITTEN!"; // TODO: Write this function.
}
1 Like

Now… you COULD just write an if statement for every letter.

You COULD also write the decode_single_letter() in the same way.

But there is a better way! (There usually is!)

We know that if the user enters " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
it should print
"#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH”.

Look at these two strings! The first has all the letters we might want to encode. The second has their encoded result - and importantly - that encode letter is in the SAME POSITION in the string.

So to encode a single letter, if we knew the position of that letter in the first string, we could get the character at that same position from the second string, and then simple return that as a result.

Hey, THAT almost sounds like code.

char encode_single_letter( char input ){
  int position = find_position( input, first_string );
  return( second_string.charAt( position ) );
}

Whoa! We now have a function that will work for any letter - and it’s only two lines of code long! All we need to do now, really, is write the function that finds the positions. But that seems like something that people might want to do a lot. Maybe there is already a function that does it.

At this point you Google it. “Processing String find”. Top result ==> https://processing.org/reference/String_indexOf_.html
Hey, that does what we want!

char encode_single_letter( char input ){
  int position = first_string.indexOf( input );
  if( position == -1 ) return '~'; // Didn't find a matching letter... Encode to a tilde.
  return( second_string.charAt( position ) );
}

Notice that I threw in a check for the situation in which the input letter was not found.

So now we can encode one letter. But the user can input MANY letters. Oh dear. Looks like it’s time to write our encode_string() function.

How will it possible work? Well, it will need to look at each letter in turn. For each letter, we will need to encode that letter. Then we will need to append that letter to a string that we will eventually return. Hey, that sounds like code!

start with an empty result string, so result is ""
for each letter {
  result = result + encode( this_letter );
}
return the result

And that’s all the help I am willing to provide. Try to edit the above procedure so it IS code. You know how a for loop works, right? If not, look in the reference and try it yourself first. You can find how many letters are in the user’s string too - you already know which function can get that for you.

Post the complete code of your attempt for more help. Please show us that you have put some serious effort into it - I’ve probably already helped you TOO much.

1 Like

yea, I got it. Now my code is running properly. And really you helped me a lot in this code. Thank you so much for your response.

1 Like

Great! Can we see the finished code (or the code you have now)? For closure.

here, I attached a part of my code.
`import javax.swing.JOptionPane;

int diameter = int(JOptionPane.showInputDialog( “Please enter your message!”));
String input=str(diameter);

void setup(){
size(500, 500);

}

void draw(){

background(0);

if (diameter != 1){
return;
}
fill(#F51B1B);
text(8,250,250);

}`
I took inspiration from your code. once again thank you. :relaxed:

ohsQmqg#g4Fh#ht54EhE#OhDO#9Gbb#F4OGMmOh#j4W#O4#94su#4t#OQGg#m#eGO#F4sh#ghsG4Wgbj###34qh6Wbbj#j4W#msh#t4O#Eh54EGt1#OQGg#ej#QmtE#eh5mWgh#G6#j4W#msh#OQht#j4W#msh#9mgOGt1#j4Ws#OGFh###NO#94WbE#eh#FW5Q#hmgGhs#O4#dWgO#9sGOh#m#Eh54Ehs#6Wt5OG4t###lmO#mO#d4hg#5sme#gQm5u#OQhj#QmMh#shmbbj#144E#1msbG5#6sGhg##N6#j4W#4sEhs#m#5Q454bmOh#FGbugQmuh#eh#gWsh#t4O#O4#hmO#OQh#5Qhssj#eh5mWgh#OQhj#msh#q4Gg4thE###xQhsh#msh#t4#gh5shOg#4s#QGtOg#Gt#OQGg#OhDO#eh5mWgh#N#mbshmEj#1mMh#j4W#m#O4t#46#Qhbq#Gt#Fj#q4gOg#mbshmEj###xQGg#Gg#OQh#htE#46#OQh#Fhggm1h#t49#g4#N#9Gbb#gmj#144Eejh###N#Q4qh#hMhsj4th#QmE#m#gm6h#mtE#Qmqqj#3mbb49hht###ch#gWsh#t4O#O4#hmO#mbb#j4Ws#5mtEj#Gt#4th#Emj###N6#j4W#E4#OQmO#j4W#9Gbb#1hO#gG5u#64s#gWsh###cpp#3m#Qm#Qm#EGE#N#g5msh#j4W###pQ#N#EGEtO###Rhbb#9QmOhMhs

I tried and succeed but there is a problem. It is just changing a first letter only, and leaves rest letters as it is. For example if user enter 01 then it converts 0 into A but leaves 1 as it is.

import javax.swing.JOptionPane; //reads message from user
String input = JOptionPane.showInputDialog( "I will draw a circle.\n"+ "What diameter do you want?"); //stores the value
                                                                                          //entered by user

String basic=" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";// string which we want to convert
String  converted="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH";// converted string


  for (int p=0; p<=basic.length(); p=+2) {
   println(basic.indexOf(input)); //prints index of the character from basic string  
   
   p=basic.indexOf(input); // stores the index number of basic string
   
    println(converted.charAt(p)); // print the character at that index number from converted string
  }

so I am confused what should I do?

Do I really have to post a link to one of my own posts in this thread?

Fine.

But I am not getting it bro. please help me because I am new to processing and I have to submit this assignment tomorrow. please! please! help me :worried:

Functions. They take input. The produce an output.

Here is a simple function:

int add_two( int in ){
  int out = in + 2;
  return( out );
}

Let’s go through this word by word.

int is what the function returns. In this case, a number.
add_two is the name of the function.
( int in ) is the input - in this case, one number, which is called in.
{ This denotes the start of the body of the function.
int out = in + 2; is a line of code. It creates a new number, called out, that has the value of in + 2.
return is what the function’s output is going to be.
( out ); is that the output is going to be the value of the variable out.
} is the end of the body of this function.

This function will add two to any number you give it as input.
add_two( 4 ) --> 6
add_two( 19 ) --> 21
Etc.

Still with me?


In your assignment, you can break down the process of encoding (or decoding) a String into smaller, simpler steps.

Instead of encoding the whole string at once, start with something simpler. Just convert one LETTER. A single character.

Once you have a function that takes a character to be encoded as input, and returns the encoded character as output, you are one step closer to having the whole process done.

This is what a function that encodes a character should look like:

char encode_single_letter( char in ){
  char out = '?'; // TODO: Encode letters properly here.
  return( out );
}

Again, follow along with what this function says it does.
char is what the function returns. In this case, a character.
encode_single_letter is the name of the function.
( char in ) is the input - in this case, one character, which is called in.
{ This denotes the start of the body of the function.
char out = '?'; is a line of code. It creates a new character, called out, that has the value of '?'.
return is what the function’s output is going to be.
( out ); is that the output is going to be the value of the variable out.
} is the end of the body of this function.

Notice that this function currently does not work like you want.
It always encodes a letter to a question mark.
For example:
encode_single_letter( ‘A’ ) --> ‘?’
encode_single_letter( ‘8’ ) --> ‘?’
encode_single_letter( ‘h’ ) --> ‘?’

Your first task, then, is to work out a way to complete this function so that it properly returns and encoded character as output based on the character that it took as input. As I have already described TWO WAYS of finishing this function in my previous posts, I will leave it up to you to get it working.


Now that we have a working function that takes a letter and returns the encoded letter, we just need to loop over every letter in the input String, and encode each letter in turn. We can - and should! - write a second function the encodes an entire String:

String encode_string( String in ){
  String out = "???"; // TODO: Fix this function!
  // You will need a loop that loops over each letter in the input string!
  // You will need to call encode_single_letter() with each character!
  return( out );
}

This function should look familiar, as I have also described how it should work already, in previous posts.

Please at least try putting some effort in to writing these two functions. Post the code of your attempt at finishing them. It is hard to impossible for us to help you unless we can see you making progress & what you are having trouble understanding.

1 Like

hey! :joy: I am getting output with this code below.

import javax.swing.JOptionPane; //reads message from user
String input = JOptionPane.showInputDialog( "I will draw a circle.\n"+ "What diameter do you want?"); //stores the value
                                                                                          //entered by user

String      basic=" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";// string which we want to convert
String  converted="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH";// converted string

int i=0;
while(i<=(input.length()-1)){
   char j=input.charAt(i);
   //println(j);
   i++;
   if(j==' '){
     println('#');
   }
   if(j=='0'){
     println('A');
   }
   if(j=='1'){
     println('8');
   }
   if(j=='2'){
     println('f');
   }
 }

but how can I print it on the canvas using text.!!! :thinking:

I do not say these things for you to ignore them…

1 Like

yea, you are right but in my code there are lots of println statements.


import javax.swing.JOptionPane; //reads message from user
String input = JOptionPane.showInputDialog( "I will draw a circle.\n"+ "What diameter do you want?"); //stores the value
                                                                                          //entered by user
String      basic=" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";// string which we want to convert
String  converted="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH";// converted string

void setup(){
 size(500,500); 
}
void draw(){
int i=0;
while(i<=(input.length()-1)){
   char j=input.charAt(i);
   //println(j);
   i++;
   
   char out;
   if(j==' '){
     println('#');
   }
   if(j=='0'){
     println('A');
   }
   if(j=='1'){
     println('8');
   }
   if(j=='2'){
     println('f');
   }
   if(j=='3'){
     println('Z');
   }
   if(j=='4'){
   println('k');
   }
   if(j=='5'){
     println('n');
   }
   if(j=='6')
   println('V');
   
   if(j=='7')
   println('v');
 
   if(j=='8'){
     println('0');
   }
   if(j=='9'){
     println('T');
   }
   if(j=='a'){
     println('m');
   }
   if(j=='b'){
     println('e');
   }
   if(j=='c'){
     println('5');
   }
   if(j=='d'){
     println('E');
   }
   if(j=='e'){
     println('h');
   }
   if(j=='f'){
    println('6'); 
   }
   if(j=='g'){
     println('1');
   }
   if(j=='h'){
     println('Q');
   }
   if(j=='i'){
    println('G'); 
   }
   if(j=='j'){
    println('d'); 
   }
   if(j=='k'){
    println('u'); 
   }
   if(j=='l'){
    println('b'); 
   }
   if(j=='m'){
    println('F'); 
   }
   if(j=='n'){
    println('t'); 
   }
   if(j=='o'){
    println('4'); 
   }
   if(j=='p'){
    println('q'); 
   }
   if(j=='q'){
    println('B'); 
   }
   if(j=='r'){
    println('s'); 
   }
   if(j=='s'){
    println('g'); 
   }
   if(j=='t'){
    println('O'); 
   }
   if(j=='u'){
    println('W'); 
   }
   if(j=='w'){
    println('9'); 
   }
   if(j=='x'){
    println('D'); 
   }
   if(j=='y'){
    println('j'); 
   }
   if(j=='z'){
    println('I'); 
   }
   if(j=='A'){
    println('r'); 
   }
   if(j=='B'){
    println('c'); 
   }
   if(j=='C'){
    println('L'); 
   }
   if(j=='D'){
    println('z'); 
   }
   if(j=='E'){
    println('l'); 
   }
   if(j=='F'){
    println('U'); 
   }
   if(j=='G'){
    println('J'); 
   }
   if(j=='H'){
    println('3'); 
   }
   if(j=='I'){
    println('N'); 
   }
   if(j=='J'){
    println('S'); 
   }
   if(j=='K'){
    println('a'); 
   }
   if(j=='L'){
    println('X'); 
   }
   if(j=='M'){
    println('7'); 
   }
   if(j=='N'){
    println('w'); 
   }
   if(j=='O'){
    println('p'); 
   }
   if(j=='P'){
    println('o'); 
   }
   if(j=='Q'){
    println('i'); 
   }
   if(j=='R'){
    println('C'); 
   }
   if(j=='S'){
    println('2'); 
   }
   if(j=='T'){
    println('x'); 
   }
   if(j=='U'){
    println('P'); 
   }
   if(j=='V'){
    println('Y'); 
   }
   if(j=='W'){
    println('R'); 
   }
   if(j=='X'){
    println('k'); 
   }
   if(j=='Y')
    println('y'); 
   
   if(j=='Z')
    println('H'); 
   
//  for (int p=0; p<=basic.length(); p++) {
//   println(basic.indexOf(input)); //prints index of the character from basic string  
   
//   int store=basic.indexOf(input); // stores the index number of basic string
   
//    println(converted.charAt(store)); // print the character at that index number from converted string
}
}

so it is a bit confusing! how to combine those printlns and convert them into text

What’s really confusing is why you bothered to write out an if statement for every character. Didn’t your hand start cramping up after the first ten cases? Didn’t you consider that there might be a better way? Hell, I even TOLD YOU that there WAS a better way.

If you had followed along with my initial posts at all, you would have been able to spare yourself a lot of time and just write the encoding function with a simple loop. Here, look at this example:

import javax.swing.JOptionPane;
String user_string, encoded_string;

String      basic = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Unencoded
String  converted = "#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH"; // Encoded


void setup(){
  size(600,400);
  textSize(22);
  user_string = JOptionPane.showInputDialog("Please Enter Your Message!");
  if( user_string == null ){
    user_string = "";
  }
  encoded_string = encode_string( user_string );
}

void draw(){
  background(0);
  fill(255);
  text(user_string, 20, 40, width-40, height/2 - 80);
  text(encoded_string, 20, height/2 + 40, width-40, height/2 - 80);
}

char encode_single_letter( char input ){
  int position = basic.indexOf( input );
  if( position == -1 ) return '~'; // Didn't find a matching letter... Encode to a tilde.
  return( converted.charAt( position ) );
}

String encode_string( String input ){
  // This will be the output.
  String output = "";
  // TODO: Remove this notice from the encoded String. It is just here as an example of string concatination!
  output = output + "The encoded string is: ERROR - Encoding function needs to be written!";
  
  // TODO: Loop over every character in the input. Use a for loop, or even a while loop!
  
  // TODO: Encode that character. Call the encode_single_letter() function!
  
  // TODO: Append the encoded character to the output. Do string concatenation!
 
  // Return the output.
  return( output );
}

That is the final example I am giving you. I’m done. If you can’t learn enough about loops, function calls, and string concatenation on your own to be able to finish this assignment now, you deserve to fail.

1 Like