如何解决 Java BufferString 问题?我的意思是清除缓冲区
How to solve Java BufferString problem? I mean clearing buffer
我的代码没有什么问题,我将 Java 字符串拆分为 table,然后我选择了选定的字符并将其替换为 StringBuffer 方法。
问题是当我在提取的 toString() 的最后使用 sout 方法时,我的加密值会自动显示为我在开始时想要加密的原始值。我尝试了很多清除 BufferedStream 但无法解除它。
另见下图代码
enter image description here
public class third {
public static void main(String[] args) {
String letter = "drukarka";
String[] tab1 = letter.split("");
String result = "Result of cypher: ";
StringBuffer cypher = new StringBuffer("drukarka");
for (int i = 0; i < tab1.length; i++) {
String word = tab1[i];
int k = tab1.length;
k--;
if (word.equals("d")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "#");
} else if (word.equals("r")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "!");
} else if (word.equals("u")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "$");
} else if (word.equals("k")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "&");
} else if (word.equals("a")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), ";");
}
}
String[] cypher2 = cypher.toString().split("");
StringBuffer cypher3 = new StringBuffer("");
for (int i = 0; i < cypher2.length; i++) {
cypher3.append(cypher2[i]);
}
String cyphered = String.valueOf(cypher3);
cypher.setLength(0);
cypher3.setLength(0 );
thirdd construction = new thirdd();
construction.constructor(letter, result, cyphered);
System.out.println(construction.toString());
}
}
你的代码有点奇怪。您想要替换 cypher
中的字母,但您并没有替换它们。相反,您似乎总是在字符串的末尾插入替换项。
我想你想要这样的东西(未测试)
public class third {
public static void main(String[] args) {
String input = "drukarka";
StringBuffer cypher = new StringBuffer();
// Get each character in the input string
for (int i = 0; i < input.length; i++) {
// Append a new cypher character depending on the input character
switch (input.charAt(i))
case 'd':
cypher.append('#');
break;
case 'r':
cypher.append('!');
break;
case 'u':
cypher.append('$');
break;
case 'k':
cypher.append('&');
break;
case 'a':
cypher.append(';');
break;
}
}
String result = cypher.toString();
// Rest of the code skipped because I'm not sure what you are trying to do.
}
}
或者更简单的版本:
public class third {
public static void main(String[] args) {
String input = "drukarka";
StringBuffer cypher = new StringBuffer();
HashMap<Character, Character> charMap = new HashMap<>();
// Create a map that maps an input character to its cypher version
charMap.add('d', '#');
charMap.add('r', '!');
charMap.add('u', '$');
charMap.add('k', '&');
charMap.add('a', ';');
for (int i = 0; i < input.length; i++) {
// look up each character in the charMap and
// append it to our output cypher
cypher.append( charMap.get(input.charAt(i)) );
}
String result = cypher.toString();
}
}
我的代码没有什么问题,我将 Java 字符串拆分为 table,然后我选择了选定的字符并将其替换为 StringBuffer 方法。 问题是当我在提取的 toString() 的最后使用 sout 方法时,我的加密值会自动显示为我在开始时想要加密的原始值。我尝试了很多清除 BufferedStream 但无法解除它。 另见下图代码 enter image description here
public class third {
public static void main(String[] args) {
String letter = "drukarka";
String[] tab1 = letter.split("");
String result = "Result of cypher: ";
StringBuffer cypher = new StringBuffer("drukarka");
for (int i = 0; i < tab1.length; i++) {
String word = tab1[i];
int k = tab1.length;
k--;
if (word.equals("d")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "#");
} else if (word.equals("r")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "!");
} else if (word.equals("u")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "$");
} else if (word.equals("k")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), "&");
} else if (word.equals("a")) {
cypher.replace(Integer.valueOf(tab1.length-k-1), Integer.valueOf(tab1.length-k-1), ";");
}
}
String[] cypher2 = cypher.toString().split("");
StringBuffer cypher3 = new StringBuffer("");
for (int i = 0; i < cypher2.length; i++) {
cypher3.append(cypher2[i]);
}
String cyphered = String.valueOf(cypher3);
cypher.setLength(0);
cypher3.setLength(0 );
thirdd construction = new thirdd();
construction.constructor(letter, result, cyphered);
System.out.println(construction.toString());
}
}
你的代码有点奇怪。您想要替换 cypher
中的字母,但您并没有替换它们。相反,您似乎总是在字符串的末尾插入替换项。
我想你想要这样的东西(未测试)
public class third {
public static void main(String[] args) {
String input = "drukarka";
StringBuffer cypher = new StringBuffer();
// Get each character in the input string
for (int i = 0; i < input.length; i++) {
// Append a new cypher character depending on the input character
switch (input.charAt(i))
case 'd':
cypher.append('#');
break;
case 'r':
cypher.append('!');
break;
case 'u':
cypher.append('$');
break;
case 'k':
cypher.append('&');
break;
case 'a':
cypher.append(';');
break;
}
}
String result = cypher.toString();
// Rest of the code skipped because I'm not sure what you are trying to do.
}
}
或者更简单的版本:
public class third {
public static void main(String[] args) {
String input = "drukarka";
StringBuffer cypher = new StringBuffer();
HashMap<Character, Character> charMap = new HashMap<>();
// Create a map that maps an input character to its cypher version
charMap.add('d', '#');
charMap.add('r', '!');
charMap.add('u', '$');
charMap.add('k', '&');
charMap.add('a', ';');
for (int i = 0; i < input.length; i++) {
// look up each character in the charMap and
// append it to our output cypher
cypher.append( charMap.get(input.charAt(i)) );
}
String result = cypher.toString();
}
}