0

Below you will find me using toCharArray in order to send a string to array. I then MOVE the value of the letter using a for statement...

for(i = 0; i < letter.length; i++){
               letter[i] += (shiftCode);
               System.out.print(letter[i]);
            }

However, when I use shiftCode to move the value such as...

a shifted by -1; I get a symbol @. Is there a way to send the string to shiftCode or tell shiftCode to ONLY use letters? I need it to see my text, like "aaron", and when I use the for statement iterate through a-z only and ignore all symbols and numbers. I THINK it is as simple as...

letter=codeWord.toCharArray(a,z);

But trying different forms of that and googling it didn't give me any results. Perhaps it has to do with regex or something? Below you will find a complete copy of my program; it works exactly how I want it to do; but it iterates through letters and symbols. I also tried finding instructions online for toCharArray but if there exists any arguments I can't locate them.

My program...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * Aaron L. Jones
 * CS219
 * AaronJonesProg3
 * 
 * This program is designed to -
 * Work as a Ceasar Cipher
 */

/**
 *
 * Aaron Jones
 */
public class AaronJonesProg3 {
    static String codeWord;
    static int shiftCode;
    static int i;
    static char[] letter;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // Instantiating that Buffer Class
        // We are going to use this to read data from the user; in buffer
        // For performance related reasons
        BufferedReader reader;

        // Building the reader variable here
        // Just a basic input buffer (Holds things for us)
        reader = new BufferedReader(new InputStreamReader(System.in));

        // Java speaks to us here / We get it to query our user
        System.out.print("Please enter text to encrypt: ");

        // Try to get their input here
        try {    
            // Get their codeword using the reader
            codeWord = reader.readLine();

            // Make that input upper case
            codeWord = codeWord.toUpperCase();
            // Cut the white space out
            codeWord = codeWord.replaceAll("\\s","");
            // Make it all a character array
            letter = codeWord.toCharArray();
        }
        // If they messed up the input we let them know here and end the prog.
        catch(Throwable t) {
            System.out.println(t.toString());
            System.out.println("You broke it. But you impressed me because"
                    + "I don't know how you did it!");
        }

        // Java Speaks / Lets get their desired shift value
        System.out.print("Please enter the shift value: ");

        // Try for their input
        try {
               // We get their number here
               shiftCode = Integer.parseInt(reader.readLine());
        }
        // Again; if the user broke it. We let them know.
        catch(java.lang.NumberFormatException ioe) {
            System.out.println(ioe.toString());
            System.out.println("How did you break this? Use a number next time!");
        }

        for(i = 0; i < letter.length; i++){
           letter[i] += (shiftCode);
           System.out.print(letter[i]);
        }

        System.out.println();

        /****************************************************************
         ****************************************************************
         ***************************************************************/
        // Java speaks to us here / We get it to query our user
        System.out.print("Please enter text to decrypt: ");

        // Try to get their input here
        try {    
            // Get their codeword using the reader
            codeWord = reader.readLine();

            // Make that input upper case
            codeWord = codeWord.toUpperCase();
            // Cut the white space out
            codeWord = codeWord.replaceAll("\\s","");
            // Make it all a character array
            letter = codeWord.toCharArray();
        }
        // If they messed up the input we let them know here and end the prog.
        catch(Throwable t) {
            System.out.println(t.toString());
            System.out.println("You broke it. But you impressed me because"
                    + "I don't know how you did it!");
        }

        // Java Speaks / Lets get their desired shift value
        System.out.print("Please enter the shift value: ");

        // Try for their input
        try {
               // We get their number here
               shiftCode = Integer.parseInt(reader.readLine());
        }
        // Again; if the user broke it. We let them know.
        catch(java.lang.NumberFormatException ioe) {
            System.out.println(ioe.toString());
            System.out.println("How did you break this? Use a number next time!");
        }

        for(i = 0; i < letter.length; i++){
           letter[i] += (shiftCode);
           System.out.print(letter[i]);
        }

        System.out.println();
    }
}
4

3 Answers 3

2

Use Character.isLetter() to separate out the Letters.......

See this link.....

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html

Eg:

public class Test{
   public static void main(String args[]){
      System.out.println( Character.isLetter('c'));
      System.out.println( Character.isLetter('5'));
   }
}

Output:

true false

Sign up to request clarification or add additional context in comments.

1 Comment

This didn't work for me; but I am pretty sure it could have worked. Up voting in case someone else takes this path. Thank you.
1

First check each character of the array with if(Character.isLetter(letter[i])). Then with your shiftcode, check the difference between the character at letter[i] with corresponding last alphabet character i.e. uppercase or a lowercase 'z'. For a detailed explanation, see my answer for a caesar cipher at this link in this forum.

1 Comment

This worked. Sort of. I didn't use isLetter; but after seeing your use of the number 26 it led me to another website that eventually gave me the answer I wanted. Upvoting this for the lead.
0
        // iterating
    for(i = 0; i < letter.length; i++){
        letter[i] += (shiftCode); // Start adding shiftcode to letter
        if(!(letter[i] <= 'Z')) {
            // if its bigger than Z subtract 26
            letter[i] -= (26);
        }
        // less than A?
        if(!(letter[i] >= 'A')) {
            // add 26
            letter[i] += (26);
        }
        System.out.print(letter[i]); // print it all out
    }

The above code is the correct answer to my problem. I discovered that UNICODE is numbered and by using the number 26 we could move about PRETTY well if you don't go hog wild with choosing a large number. I think a smart thing to do would be to program in a catch that forces the user to only iterate by up to -26 to 26.

1 Comment

I find it funny to read the mix of char and int types. You could do it using the fact that ('Z' - 'A') == 25 e.g. final char ADJUST = ('Z' - 'A') + 1; then use ADJUST in your for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.