Java替换方法问题

时间:2016-02-08 19:27:52

标签: java string encryption replace

我正在尝试为我的计算机科学课做一个名为"加密/解密的项目"

代码如下

where invoice_balance <> 0

问题不在import java.io.*; import java.util.*; import java.io.*; import java.util.*; public class Tester { public static void main(String args[]) { Scanner kbReader = new Scanner(System.in); System.out.print("Enter a sentence that is to be encrypted: "); String sntnc = kbReader.nextLine(); System.out.println("Original Sentence = " + sntnc); Crypto myCryptObj = new Crypto(); String encryptdSntnc = myCryptObj.encrypt(sntnc); System.out.println("Encrypted sentence = " + encryptdSntnc); String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc); System.out.println("Decrypted sentence = " + decryptdSntnc); } } class Crypto { public String encrypt(String sntnc) { sntnc = sntnc.replace("m", "ssad"); sntnc = sntnc.replace("b", "dug>?/"); sntnc = sntnc.replace("g", "jeb..w"); sntnc = sntnc.replace("v", "ag',r"); return sntnc; } public String decrypt(String sntnc) { sntnc = sntnc.replace("ag',r", "v"); sntnc = sntnc.replace("ssad", "m"); sntnc = sntnc.replace("jeb..w", "g"); sntnc = sntnc.replace("dug>?/", "b"); return sntnc; } } 课程中,而是Tester课程。

输入为:Crypto 代码应该输出:

This is a very big morning.

但是Enter a sentence that is to be encrypted: This is a very big morning. Original Sentence: This is a very big morning. Encrypted sentence: This is a ag',rery dug>?/ijeb..w ssadorninjeb..w. Decrypted sentence: This is a very big morning. 行正在打印:

Encrypted sentence

This is a ag',rery dujeb..w>?/ijeb..w ssadorninjeb..w. 方法正在替换已经替换的字母。 如何将其修复到两次取代的地方?

3 个答案:

答案 0 :(得分:6)

问题来自你更换字符串的逻辑:

sntnc = sntnc.replaceAll("b", "dug>?/");  // <-- replaces with a "g" here
sntnc = sntnc.replaceAll("g", "jeb..w");  // <-- so that "g" is also getting replaced here

一个简单的解决方案是使用replaceAll。通过重新订购replaceAll电话来使您的代码有效是非常脆弱的:您不知道将来有人会改变被替换的内容并破坏所有内容。

通过迭代字符构建String时,解决方案更加简单。因为在构建字符串时我们有一个独特的临时容器,所以在替换发生时没有冲突。

public String encrypt(String sntnc) { 
     StringBuilder sb = new StringBuilder();
     for (char ch : sntnc.toCharArray()) {
         if (ch == 'm') sb.append("ssad");
         else if (ch == 'b') sb.append("dug>?/");
         else if (ch == 'g') sb.append("jeb..w");
         else if (ch == 'v') sb.append("ag',r");
         else sb.append(ch);
     }
     return sb.toString();
} 

答案 1 :(得分:0)

而不是使用sntnc = sntnc.replaceAll("m", "ssad"); 尝试使用适当的加密代码替换每个字符,然后将其保存在另一个字符串中并将其返回。

`String s = "xyz";
for(int i = 0; i < sntnc.length(); i++)
{
   char c = sntnc.charAt(i);
   // Do your code here saving it in a new string say sntnc2
}`
   return sntnc2;

可能不是最有效的方法,但使用replaceAll("a","c")已完成,从字符串的开头到结尾用a替换每个字符c

答案 2 :(得分:0)

你的问题是一个替换与其他替换:

sntnc = sntnc.replace("g", "jeb..w"); 
sntnc = sntnc.replace("b", "dug>?/");

第一个包含b,第二个包含g甚至重新排序,替换将无效。

您必须立即进行所有替换,请尝试:

class Crypto { 
 public String encrypt(String sntnc) {    

    Pattern p = Pattern.compile("(m|g|b|v)");
    Matcher m = p.matcher(sntnc);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        if(m.group(1).equals("m"))
            m.appendReplacement(sb, "ssad");
        if(m.group(1).equals("g"))
            m.appendReplacement(sb, "jeb..w");
        if(m.group(1).equals("b"))
            m.appendReplacement(sb, "dug>?/");
        if(m.group(1).equals("v"))
            m.appendReplacement(sb, "ag',r");
    }

    m.appendTail(sb);

    return sb.toString(); 
}