【发布时间】:2013-07-30 20:32:59
【问题描述】:
第一次来。我正在尝试编写一个程序,该程序从用户那里获取字符串输入并使用 replaceFirst 方法对其进行编码。除“`”(重音符号)外的所有字母和符号都能正确编码和解码。
例如当我输入
`12
我应该得到 28AABB 作为我的加密,但它给了我 BB8AA2
public class CryptoString {
public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
String input = "";
input = JOptionPane.showInputDialog(null, "Enter the string to be encrypted");
JOptionPane.showMessageDialog(null, "The message " + input + " was encrypted to be "+ encrypt(input));
public static String encrypt (String s){
String encryptThis = s.toLowerCase();
String encryptThistemp = encryptThis;
int encryptThislength = encryptThis.length();
for (int i = 0; i < encryptThislength ; ++i){
String test = encryptThistemp.substring(i, i + 1);
//Took out all code with regard to all cases OTHER than "`" "1" and "2"
//All other cases would have followed the same format, except with a different string replacement argument.
if (test.equals("`")){
encryptThis = encryptThis.replaceFirst("`" , "28");
}
else if (test.equals("1")){
encryptThis = encryptThis.replaceFirst("1" , "AA");
}
else if (test.equals("2")){
encryptThis = encryptThis.replaceFirst("2" , "BB");
}
}
}
我已尝试将转义字符放在重音前面,但是仍然无法正确编码。
【问题讨论】:
-
你真的有没有
if的else吗? -
您的代码无法编译。发布您正在使用的真实代码会更有帮助。
-
我删除了所有其他案例,只显示了这个特定案例。
-
此外,
encryptThis.replaceFirst("`", "28")正确地将`替换为"28"。 -
不清楚你的加密应该如何处理其他字符,你的
else if没有前面的if