I am a beginner, please help!

i need to make something in which when user inputs some string it got converted into another sets of string

Hello and welcome to the forum !
Great to have you here!!

Basically when you have two Strings (source and cipher):

  • for-loop over the user input (another String) :

  • Use charAt() and indexOf() to get the position of the current letter in the first long String source. Store it in a variable int myIndex

  • Use myIndex with charAt() in the 2nd long String cipher with println() please

Show your code (as far as you got) so we can help you.

Warm regards,

Chrisir

1 Like

So Far I wrote this and its giving error. I really dont know what else to change. Please help

import javax.swing.JOptionPane;
//String user;
String new_String="";

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

void draw(){
String source=“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
String cipher="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH" ;
String user = JOptionPane.showInputDialog(null, "Type anything you need to covert into cipher ");

for (int i=0; i<user.length(); i++)
char current= user.charAt(i);

for(int j=0; j<source.length(); j++){
if(source.charAt(j) == current){
new_String += cipher.charAt(j);

   println(new_String);
  
   }

}
}

Does it work? What does it do wrong?

Check my previous post again

1 Like

char current= user.charAt(i); >>>>>>
At this particular line there is an error : Syntax error, insert “. class” to complete Expression.

As far as I know it should run ! but still not working :frowning:

here…

you need to declare current before setup() so it’s a global var

doesn’t do what you want of course


import javax.swing.JOptionPane;
//String user;
String new_String="";

char current1;

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

void draw() {
  String source="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  String cipher="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9DjIrcLzlUJ3NSaX7wpoiC2xPYRkyH" ;
  String user = JOptionPane.showInputDialog(null, "Type anything you need to covert into cipher ");

  for (int i=0; i<user.length(); i++)
    current1 = user.charAt(i);

  for (int j=0; j<source.length(); j++) {
    if (source.charAt(j) == current1) {
      new_String += cipher.charAt(j);
      println(new_String);
    }
  }
}

Thanks a Lot to you brother . I got it now <3

1 Like

well done, congratulations!

Mind showing your code?

1 Like

please note, the name of the function is setup() (not size)

void setup() { // !!!
  size(500, 500);
}
1 Like

<<<<<<<<<<<<Thanks for your effort. I saw it later.
I have another question like there is a bug. When I input multiple strings at a time it only gives the output for one only. Such as if I type only one one letter then the output is fine but if I type my name “aziz” then the output is “j” which means it is giving the output for the last letter of my name only. But I wanted to make it in such a way so that the output is equal to the user input. >>>>>>>>>>>>>>

import javax.swing.JOptionPane;
String user;
String new_String="";
char current;

void setup(){
size(1000,1000);
}

void draw(){
String source=“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
String cipher="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9Dj IrcLzlUJ3NSaX7wpoiC2xPYRkyH" ;
String user = JOptionPane.showInputDialog(null, "Type anything you need to covert into cipher ");

for (int i=0; i<user.length(); i++)
current= user.charAt(i);

for(int j=0; j<source.length(); j++){
if(source.charAt(j) == current){
new_String += cipher.charAt(j);

println(new_String);
background(160);

textSize(40);
text(new_String, 30,40);

}
}
}

You need { after this line

and close it before the line with text() with }

1 Like

Sorry I tried that but Didn’t work its still only giving output for the single inputs . I made some change in the code though now.

import javax.swing.JOptionPane;
String user;
String new_String="";
char current;

void setup(){
size(1000,1000);
background(150);
}

void draw(){
String source=“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
String cipher="#A8fZKnVv0Tme5Eh61QGdubFt4qBsgOWM9Dj IrcLzlUJ3NSaX7wpoiC2xPYRkyH" ;
String user = JOptionPane.showInputDialog(null, "Type anything you need to covert into cipher ");

for (int i=0; i<user.length(); i++)
current= user.charAt(i);

for(int j=0; j<source.length(); j++){
if(source.charAt(j) == current){
new_String += cipher.charAt(j);

view();

}
}
}

void view(){
println(new_String);
textSize(40);
text(new_String, 30,40);
}

Believe me when I say you need those {}

You know, in the first for-loop you loop over the letters of the user input and evaluate current.

But the first loop at the moment is only

  for (int i=0; i<user.length(); i++)
    current= user.charAt(i);

With the brackets { } you can extend how far the for loop reaches.

It must encompass the whole area and the next for-loop. So place the } after the 2nd for-loop.

It works here by the way.

Besides, check out indexOf() command in the reference, you don’t really need the 2nd for-loop: https://www.processing.org/reference/String_indexOf_.html

Warm regards,

Chrisir

1 Like