Hey guys! good evening!
hope you can help me with this one.
this is the thing that i want to do!:

-Input 2 values : 1 string, and 1 int
-the string should have only letters (cant contain numbers!)
-encrypt the string using the following conditions:
-use the int as encryption
key.
-the key tells how far in the alphabet the String will adjust.

so for example,
enter number : 2
enter name: aAa

output: cCc

this is my code:

int encryptor = 0;
String input = " ";
JOptionPane.showMessageDialog(null,"Welcome");
encryptor = Integer.parseInt(JOptionPane.showInputdialog(null,"Please enter number: ");
input = JOptionPane.showInputDialog(null,"Enter Name: ");

Hey guys! good evening!
hope you can help me with this one.
this is the thing that i want to do!:

-Input 2 values : 1 string, and 1 int
-the string should have only letters (cant contain numbers!)
-encrypt the string using the following conditions:
-use the int as encryption
key.
-the key tells how far in the alphabet the String will adjust.

so for example,
enter number : 2
enter name: aAa

output: cCc

this is my code:

int encryptor = 0;
String input = " ";

JOptionPane.showMessageDialog(null,"Welcome");
encryptor = Integer.parseInt(JOptionPane.showInputdialog(null,"Please enter number: ");

String name= "";
for(int i=0;i<input.length();i++){
    input.charAt(i);

    int x = (int) input.charAt(i);
    x=x+encryptor;
    char y = (char) x;
    name = name +y; 
}
    JOptionPane.showMessageDialog(null,name);

and aside from that, i still have a problem for example
the user will input 1 as an encryptor
and inputted 'z'
it should print 'a'
meaning it would go back to the beginning and i dont know how to do that
im using ASCII
thanks guys

Recommended Answers

All 8 Replies

After x=x+encryptor; you can test to see if x>'z' and if it is then subtract 26

commented: where should i subtract 26? +2

where should i subtract 26 ? @JamesCherrill?

I don't know how to answer that without just giving you the code (which I won't do for homework). Re-read what I wrote then think about it a bit - there's only one right answer.

this is what i did but it still wont work"

for(int i=0;i<input.length();i++){
            input.charAt(i);
            int x = (int) input.charAt(i);
            x=x+encryptor;
            if(x > 'z'){
                over=x-26;
                char r = (char) over;
                over1=over1+r;
            }


            char y = (char) x;
             name = name +y; 


        }
            JOptionPane.showMessageDialog(null,name+over);

    }

OK. There's no need for so many variables. All you need is
if(x > 'z') x = x - 26;

wow that was amazing haha ..how about if i input
something like this:
aAa
encryption:
1
output:
bBb

how can i make the case of the letters of the output same as of the input?
i dont know how to do that

If you want to use mixed-case letters like that then you need a more complicared if test - first test to see if your letter is upper or lower case. If it's lower case then use the code you just added, if it's upper case then use the same code but with 'Z' instead of 'z'.
YOu can test if a char is upper or lower case by comparing it with 'Z' - all upper case are <='Z', all lower case are >'Z' in ASCII

oh thanks ive done it thank you! @James Cherrill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.