hey guys i have done my codes and its working properly
the only thing that's bothering me is that'
when i input 52 and 'a' as my name, it wont ouput the right answer (it should be 'a' also)
i hope you guys can help me thanks!

public static void main (String args[]){
        int encryptor = 0;
        String input = " ";
        JOptionPane.showMessageDialog(null,"Welcome");
        encryptor = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter number: "));
        input =JOptionPane.showInputDialog(null,"Enter Name: ");

        String name= "";

        for(int i=0;i<input.length();i++){
            input.charAt(i);
            int x = (int) input.charAt(i);
            x=x+encryptor;
            if( x > 'z'){ // small letter
                x=x-26;
            }
            else if (x > 'Z' && x < 97){ //big letter
                x=x-26;
            }
            char y = (char) x;
            name = name +y; 
        }
        JOptionPane.showMessageDialog(null,name);
    }
}

Recommended Answers

All 11 Replies

  1. You need to determine whether the original letter was upper or lower case before you add the encryptor to it. Eg adding a suitable encryptor to an upper case may give you a valid lower case letter
  2. If the encryptor can be >26 then instead of just subtractiong one 26 in an if test you need to repeatedly subtract 26 in a while loop until the result is in the right range.
  3. Line 17 - using 97 is deliberately obscure when you really mean 'a'

yep..thats what im tryiing to know..how to know if the letter is an UPPER or LOWER case :(

You know that, for the purpose of this exercise, lower case letters are between 'a' and 'z' inclusive, upper case letters are between 'A' and 'Z', so it's just a simple if test.

i got it..but when i input more than one character the results wont show ( i erased my codes for the a-z
because i cant see my code clearly
)
hre is my code:

if (input.equals("A")|| input.equals("Z")){

            for(int i=0;i<input.length();i++){
                input.charAt(i);
                int x = (int) input.charAt(i);
                x=x+encryptor;
                if( x > 'Z'){ // small letter
                    for(int y=0;x>'Z';y++){
                        x=x-26;

                    }

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

            }
        }               



                JOptionPane.showMessageDialog(null,name);
            }
        }

line 6 is exactly what you should NOT do - add the encryptor before checking whether the char is upper or lower case.

for(int y=0;x>'Z';y++){
since you don't use y in the loop, this can be better, and a lot more clearly, written as
while(x>"Z") {

line 1 I simply do not understand.

i thought that putting this

if (input.equals("A")|| input.equals("Z"))

:would make all of them Capital letters do those statements that are inside that if statement..

not understand.

When your question has been answered please mark your thread "solved"; don't just leave it pending forever.Show us what you are capable of! Enter the DaniWeb code contest

JamesCherrill
Posting Genius
Moderator
6,544 posts
since Apr 2008

i t

That says if the letter is "A" or if it is "Z" - but what about B..Y? Your if test needs to check between 'A' and 'Z' inclusive.
Also watch out: "A" is a String, 'A' is a char - they're completely different things.

does that mean that || should be &&?

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

        String name= "";

        for(int firstLoop=0;firstLoop<input.length();firstLoop++){
            input.charAt(firstLoop);//i changed String to char here
            int x = (int) input.charAt(firstLoop);//changed char to int
            char r = (char) x;//brought it back to char
            String o = Character.toString(r);//and changed it to string so that it will            //be able to get inside the if statement
            x=x+encryptor;
            if (input.equals("A")|| input.equals("Z")){//determines whether it ranges from A-Z

                for(int secondLoop=0;secondLoop<input.length();secondLoop++){

                    input.charAt(secondLoop);//changes String to chaar
                    int charToInt = (int) input.charAt(secondLoop);//changes char to int


                        while(charToInt > 'Z'){//then this is what u told me to do
                            charToInt=charToInt-32;
                        }
                        char y = (char) charToInt;
                        name = name +y; 


                }   




            }




        }
            JOptionPane.showMessageDialog(null,name);

    }

You don't need me to answer that. What letter can be 'A' and also be 'Z' ?
The answer requires the use of >= and <=

yep
if i use that
if (x >= 'a' && x <= 'z')
then if my encryption will be 52,
and i inputted 'a'
it should be also 'a' but by using that i am only letting those numbers ranging from 90-122 to get into my program

and thats my problem :(

public class Runner{
    public static void main (String args[]){
        int encryptor = 0;
        String input = " ";
        JOptionPane.showMessageDialog(null,"Welcome");
        encryptor = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter number: "));
        input =JOptionPane.showInputDialog(null,"Enter Name: ");

        String name= "";

        for(int i=0;i<input.length();i++){
            input.charAt(i);
            int x = (int) input.charAt(i);
            x=x+encryptor;
            if( x > 'z'){ // small letter
                x=x-26;
            }
            else if (x > 'Z' && x < 'a'){ //big letter
                x=x-26;
            }
            char y = (char) x;
            name = name +y; 
        }
        JOptionPane.showMessageDialog(null,name);
    }
}

as u can see there that program outputs right answers but it does not work when i input something like 52 .. thats the only problem that i cant solve

We already discussed that one, and there's a solution in one of the earlier posts!

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.