We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,668 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Machine problem

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

2
Contributors
8
Replies
1 Hour
Discussion Span
11 Months Ago
Last Updated
9
Views
Question
Answered
Ms New to Java
Junior Poster
177 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

where should i subtract 26 ? @JamesCherrill?

Ms New to Java
Junior Poster
177 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

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);

    }
Ms New to Java
Junior Poster
177 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

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

Ms New to Java
Junior Poster
177 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

oh thanks ive done it thank you! @James Cherrill

Ms New to Java
Junior Poster
177 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 11 Months Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0858 seconds using 2.72MB