我需要用字母表中的下一个字母替换文件中的每个字母(没有使用数字,所以不用担心)。但是我应该保留空格和标点符号。该代码可以很好地更改字母,但我不确定如何保留空格和标点符号。如有任何帮助,我们将不胜感激。
import java.io.*;
import java.util.*;
class Thirteen{
public static void main(String [] args)throws IOException{
Scanner keys = new Scanner(System.in);
Scanner keystwo;
System.out.println("Please enter an input file name");
String filename = keys.next();
File infile = new File(filename);
keystwo = new Scanner(infile);
System.out.println("Please enter an output filename");
String outputfile = keys.next();
File outfile = new File(outputfile);
FileOutputStream outstream = new FileOutputStream(outfile);
PrintWriter display = new PrintWriter(outstream);
while(keystwo.hasNext()){
String one = keystwo.next().toLowerCase();
//convert to stringbuilder to edit
StringBuilder fine = new StringBuilder(one);
for(int A = 0; A < fine.length(); A++){
//read in as character
char B = fine.charAt(A);
//convert to ASCII
int two = (int)B;
//add one
int convert = two+1;
//convert to char
char C = (char)convert;
if(C == '{'){
C = 'a';
}
//print char & add to output file
display.print(Character.toString(C));
System.out.print(Character.toString(C));
}//end for
}//end while
keystwo.close();
display.close();
}
}//end
最佳答案
在将转换应用于字符串中的当前字符之前,您可以检查它是否是一个字母,如下所示:
if(Character.isLetter(character))
{
//Do conversion here
}
这样转换将仅应用于字母。
关于java - 将字符串中的每个字母替换为字母表中的下一个字母,但保留空格和标点符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41133653/