gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:13:25 26 4
gpt4 key购买 nike

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

最佳答案

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

输出:

真实错误

关于java - toCharArray 中仅使用字母数字字符 (a-z),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12333059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com