在toCharArray中仅使用字母数字字符(a-z)

时间:2012-09-08 17:44:28

标签: java string

下面你会发现我使用toCharArray来发送一个字符串到数组。然后我使用for语句移动字母的值......

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

但是,当我使用shiftCode移动诸如...的值时

a偏移-1;我得到一个符号@。有没有办法将字符串发送到shiftCode或告诉shiftCode只使用字母?我需要它来查看我的文本,比如“aaron”,当我使用for语句时只迭代a-z并忽略所有符号和数字。我认为它就像......一样简单。

letter=codeWord.toCharArray(a,z);

但尝试不同形式的谷歌搜索并没有给我任何结果。也许它与正则表达式有什么关系?您将在下面找到我的计划的完整副本;它完全符合我的要求;但它通过字母和符号进行迭代。我也尝试在线查找toCharArray的说明,但是如果有任何参数我找不到它们。

我的节目......

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

3 个答案:

答案 0 :(得分:2)

使用Character.isLetter()分隔字母.......

请参阅此链接.....

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

<强>例如

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

<强>输出:

真 假

答案 1 :(得分:1)

首先使用if(Character.isLetter(letter[i]))检查数组的每个字符。然后使用您的移位代码,检查字母[i]处的字符与相应的最后一个字母字符(即大写字母或小写字母“z”)之间的差异。有关详细说明,请参阅本论坛link中关于凯撒密码的答案。

答案 2 :(得分: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
    }

以上代码是我问题的正确答案。我发现UNICODE是编号的,使用数字26我们可以很好地移动PRETTY如果你不选择大量的野生。我认为一个明智的做法是在一个强制用户只能迭代-26到26的捕获中编程。