【问题标题】:Using only alphanumeric characters(a-z) inside toCharArray在 toCharArray 中仅使用字母数字字符 (a-z)
【发布时间】:2012-09-15 23:35:43
【问题描述】:

下面你会发现我使用 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();
    }
}

【问题讨论】:

标签: java string


【解决方案1】:

使用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'));
   }
}

输出:

真 假的

【讨论】:

  • 这对我不起作用;但我很确定它可能会奏效。投票以防其他人走这条路。谢谢。
【解决方案2】:

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

【讨论】:

  • 这行得通。有点。我没有使用 isLetter;但是在看到您使用数字 26 之后,它把我带到了另一个网站,最终给了我想要的答案。为领先者投票。
【解决方案3】:
        // 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,我们可以很好地移动。我认为明智的做法是编写一个强制用户最多只能迭代 -26 到 26 次的 catch。

【讨论】:

  • 我觉得混合阅读 charint 类型很有趣。您可以使用 ('Z' - 'A') == 25 的事实来做到这一点,例如final char ADJUST = ('Z' - 'A') + 1; 然后在你的 for 循环中使用 ADJUST
猜你喜欢
  • 1970-01-01
  • 2016-04-25
  • 2012-07-25
  • 1970-01-01
  • 2018-10-01
  • 2010-11-02
  • 2013-11-22
  • 2011-04-01
  • 1970-01-01
相关资源
最近更新 更多